PlayData/Java

[ Day 10 | Java ] 클래슀, 좔상 클래슀,

κΉ€μœ λ‹ˆμ½˜ 2021. 11. 23. 00:23

μΆ”μƒν΄λž˜μŠ€μ™€ μΈν„°νŽ˜μ΄μŠ€λ₯Ό μ§€λ‚œ 주에 λ‚˜κ°”μ–΄μ•Ό ν•œλ‹€.

μ œμž‘μ„ ν•΄μ•Όν•˜λŠ” μž…μž₯μ—μ„œ μ½”λ“œλŸ‰μ„ 쀄일 수 μžˆλŠ” 것은 λ™μΌν•œ 것을 μ‹Έμž‘μ•„μ„œ λ¬Άμ–΄μ£ΌλŠ” 것이 νš¨μš©κ°€μΉ˜κ°€ μžˆλŠ” μ½”λ“œ.

 

class Ani24{ void Cry() { System.out.println("μœΌμ•™"); } }

class Dog24 extends Ani24 { void Cry() { System.out.println("멍멍"); } }

class Cat24 extends Ani24{ void Cry() { System.out.println("μ•Όμ˜Ή"); } }

class Duck24 extends Ani24{ void Cry() { System.out.println("κ½₯κ½₯"); } }


public class Cla24 {

	public static void main(String[] args) {

		//개, 고영이, μ˜€λ¦¬κ°€ ν•œλ²ˆμ”© 울게 ν–ˆμœΌλ©΄ μ’‹κ² λ‹€. 
		//////////////////////////////////////////////
		
		Dog24 d1 = new Dog24(); d1.Cry();
		Cat24 c1 = new Cat24(); c1.Cry();
		Duck24 dd1 = new Duck24(); dd1.Cry();
		
		
//		Ani24 d1 = new Dog24(); d1.Cry();
//		Ani24 c1 = new Cat24(); c1.Cry();
//		Ani24 dd1 = new Duck24(); dd1.Cry();
		
		
		Ani24[] anis = {new Dog24(), new Cat24(), new Duck24()};
		for(Ani24 ani : anis) {ani.Cry();}
		
		
		
		short a1 = 10; int a2 = 20; short a3 = 30; int a4 = 40;
		int[] iArr = new int[4];
		iArr[0] = a1; iArr[1] = a2; iArr[2] = a3; iArr[3] = a4;
		
		for(int i : iArr) { System.out.println(i);}
	}

}

 

 

μ•Œκ³ λ¦¬μ¦˜ μ‹œν—˜λ³Ό λ•Œ κ³ λ €ν•΄μ•Ό ν•  μ„Έ 가지: 

1) μž…λ ₯κ°’ 예, 좜λ ₯κ°’ 예 

2) μˆ˜ν–‰μ‹œκ°„ 

3) λ©”λͺ¨λ¦¬

 

 

좔상 λ©”μ†Œλ“œ 

abstract ν΄λž˜μŠ€κ°€ ν•˜λ‚˜λΌλ„ μ„žμ—¬μžˆμœΌλ©΄, κ·Έ ν΄λž˜μŠ€λŠ” abstract ν΄λž˜μŠ€κ°€ λ˜μ–΄μ•Ό ν•œλ‹€. 

 

abstract class Ani25{
	
	abstract void cry(); // 좔상 λ©”μ†Œλ“œ --> μ„ μ–Έ 
	void shout() {System.out.println("으λ₯΄λ "); }
	void walk() {System.out.println("κ±Έμ–΄"); }//  일반 λ©”μ†Œλ“œ --> μ„ μ–Έ + κ΅¬ν˜„  
	
}

class Dog25 extends Ani25 { 
	void cry() { System.out.println("으 μ•™"); } //좔상 --> κ΅¬ν˜„ 
	void jump() {System.out.println("κΉ‘ 총");}  // ν•˜μœ„κ΅¬ν˜„ 
	void walk() {System.out.println("κ±Έμ„λž˜");} //override 
} 
public class Cla25 {

	public static void main(String[] args) {
		
		Dog25 d1 = new Dog25();
		d1.cry(); d1.shout(); d1.jump(); d1.walk();

	}

}
abstract class Ani26{ 
	abstract void Cry() ;
	Ani26(){System.out.println("--Ani26() μ‹€ν–‰--");}} 

class Dog26 extends Ani26 { 
	@Override
	void Cry() { System.out.println("멍멍"); } 
	Dog26(){System.out.println("--Dog26() μ‹€ν–‰--");}}

class Cat26 extends Ani26{ 
	@Override
	void Cry() { System.out.println("μ•Όμ˜Ή"); } 
	Cat26(){System.out.println("--Cat26() μ‹€ν–‰--");}}


class Duck26 extends Ani26{ 
	@Override
	void Cry() { System.out.println("κ½₯κ½₯"); } 
	Duck26(){System.out.println("--Duck26() μ‹€ν–‰--");}}

public class Cla26 {

	public static void main(String[] args) {
		
		Ani26[] Anis = {new Dog26(), new Cat26(), new Duck26()};
		for(Ani26 ani : Anis) {ani.Cry();}

	}}
interface iAnimal27{
	void Cry();
	void Shout();
	
}
// μΈν„°νŽ˜μ΄μŠ€μ˜ λͺ¨λ“  ν•­λͺ©μ€ λ‹€ 퍼블릭이닀. 
// μƒμ†λ°›λŠ” μͺ½μ€ λ‚΄λ €λ°›λŠ” μ ‘κ·Ό μ œν•œμžλ³΄λ‹€ 더 μž‘μ•„μ§ˆ 수 μ—†λ‹€. κ·Έλž˜μ„œ μΈν„°νŽ˜μ΄μŠ€μ—μ„œ μƒμ†λ°›λŠ” μͺ½μ€ λ‹€ νΌν”Œλ¦­. 
class Dog27 implements iAnimal27{
	public void Cry() { System.out.println("멍 멍");}
	public void Shout() { System.out.println("λ―€λ₯΄λ ");}    

	}

public class Cla27 {

	public static void main(String[] args) {
	
		Dog27 d1 = new Dog27();
		
		d1.Cry(); d1.Shout();
	}

}
interface iAni28{ 
	public abstract void Cry() ;
}
class Dog28 implements iAni28 { 
	@Override
	public void Cry() { System.out.println("멍멍"); } 
}
class Cat28 implements iAni28{ 
	@Override
	public void Cry() { System.out.println("μ•Όμ˜Ή"); } 
}

class Duck28 implements iAni28{ 
	@Override
	public void Cry() { System.out.println("κ½₯κ½₯"); } 
}
public class Cla28 {
	public static void main(String[] args) {

		iAni28[] Anis = {new Dog28(), new Cat28(), new Duck28()};
		for(iAni28 ani : Anis) {ani.Cry();}
	}
}

 

 

 

 

 

 

 

μΌλ°˜ν΄λž˜μŠ€μ—μ„œμΌλ°˜ 클래슀둜 상속 : extends

abstractν΄λž˜μ„œμ—μ„œ abstract 클래슀둜 상속: extends 

μΈν„°νŽ˜μ΄μŠ€μ—μ„œ μΈν„°νŽ˜μ΄μŠ€λ‘œ 상속: extends

abstract ν΄λž˜μŠ€μ—μ„œ 일반 클래슀둜 μƒμ†λ°›λŠ”λ‹€

 

 

 

읡λͺ… 객체 

미리 선언해두지 μ•Šμ€ 것. 

κ°€μ Έλ‹€μ“Έ λ•Œ,