자바 다 형 적 고전 예

2041 단어
 
  
   java          ,    java        
public class Polymorphism {
 public static void main(String[] args) {
  A a1 = new A();
  A a2 = new B();              //  B  ,   A  ,        showD(),    show(A){A and A } show(A){B and A}    
  B b = new B();               //B  A,  showD() show(A){B and A}    
  C c = new C();
  D d = new D();
  System.out.println(a1.show(b));       //  a1     show(A)
  System.out.println(a1.show(c));       //  a1     show(A)
  System.out.println(a1.show(d));      //  a1     show(D)
  System.out.println(a2.show(b));     //      show(A)
  System.out.println(a2.show(c));       //      show(A)
  System.out.println(a2.show(d));     //     show(D)
  System.out.println(b.show(b));       //  B show(B)
  System.out.println(b.show(c));
  System.out.println(b.show(d));
 }
}

class A{
 public String show(D obj){
  return("A and D");
 }
 public String show(A obj){
  return("A and A");
 }
}

class B extends A{
 public String show(B obj){
  return("B and B");
 }
 public String show(A obj){
  return("B and A");
 }
}

class C extends B{
}

class D extends B{
}
}
A and A
A and A
A and D
B and A
B and A
A and D
B and B
B and B
A and D

좋은 웹페이지 즐겨찾기