首页 > 编程语言 > 详细

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

时间:2020-05-04 14:26:42      阅读:62      评论:0      收藏:0      [点我收藏+]
 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

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

原文:https://www.cnblogs.com/mirai3usi9/p/12826312.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!