[20-05-04][Thinking in Java 6]Java Inheritance 4 - Upcasting
Posted mirai3usi9
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[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
以上是关于[20-05-04][Thinking in Java 6]Java Inheritance 4 - Upcasting的主要内容,如果未能解决你的问题,请参考以下文章
[20-05-04][Thinking in Java 8]Java Polymorphism 2 - Late Binding
Hacker Rank: Two Strings - thinking in C# 15+ ways
Thinking in Java & Writing in Python