μΆμν΄λμ€μ μΈν°νμ΄μ€λ₯Ό μ§λ μ£Όμ λκ°μ΄μΌ νλ€.
μ μμ ν΄μΌνλ μ μ₯μμ μ½λλμ μ€μΌ μ μλ κ²μ λμΌν κ²μ μΈμ‘μμ λ¬Άμ΄μ£Όλ κ²μ΄ ν¨μ©κ°μΉκ° μλ μ½λ.
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 ν΄λμ€μμ μΌλ° ν΄λμ€λ‘ μμλ°λλ€
μ΅λͺ κ°μ²΄
미리 μ μΈν΄λμ§ μμ κ².
κ°μ Έλ€μΈ λ,
'PlayData > Java' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
[Day11 | Java] μ΄κ±°νμ , μμΈμ²λ¦¬ (λλμ΄ μλ° λ!) (0) | 2021.11.23 |
---|---|
[Day 9 | Java] ν΄λμ€ / νμ λ³νκ³Ό λ€νμ± / (0) | 2021.11.19 |
[Day 8 | Java] ν΄λμ€ / μμ (0) | 2021.11.19 |
[Day7 | Java] ν΄λμ€ / λ©λͺ¨λ¦¬ / μΈμ€ν΄μ€λ©€λ²μ μ μ λ©€λ² (0) | 2021.11.17 |
[Day 6 | Java] λ°°μ΄ λ³΅μ¬ / λ©μλ / ν΄λμ€ (0) | 2021.11.17 |