[20-05-04][Thinking in Java 6]Java Inheritance 4 - Upcasting

 1 package test_1_4;
 2 
 3 public class Amphibian {
 4 
 5     public Amphibian(int i) {
 6         
 7         System.out.println("this is Amphibian");
 8     }
 9     
10     public void print(Amphibian i) {
11         
12         System.out.println("print Amphibian");
13         show();
14     }
15     
16     public void show() {
17         
18         System.out.println("show");
19     }
20     
21 }

 

 1 package test_1_4;
 2 
 3 public class Frog extends Amphibian{
 4     
 5     public Frog(int i) {
 6         
 7         super(i);
 8         System.out.println("this is frog");
 9     }
10     
11     public static void main(String[] args) {
12         
13         Frog frog = new Frog(1);
14         frog.print(frog);
15     }
16 }

 

结果如下:

this is Amphibian
this is frog
print Amphibian
show

上一篇:CF1324C Frog Jumps 题解


下一篇:Frog