8.1向上转型

简介:

package sedion.nsg.upcasting;
 
public enum Note {
    MIDDLE_C,C_SHARP,B_FLAT;
}

  
package sedion.nsg.upcasting;
public class Instrument {
    public void play(Note n){
        Syso.print("Instrument.play()");
    }
}

  
package sedion.nsg.upcasting;
 
public class Wind extends Instrument{
    public void play(Note n){
        Syso.print("Wind.play()");
    }
}

  
package sedion.nsg.upcasting;
 
public class Music {
    public static void tune (Instrument i){
        i.play(Note.MIDDLE_C);
    }
    public static void main(String[] args) {
        Wind flute = new Wind();
        tune(flute);
    }
}

相关文章
|
5月前
|
SQL 数据库
父类坑
父类坑
25 0
|
9月前
|
C#
C#方法重写
C#方法重写
38 0
|
5月前
|
编译器
【多态-动态绑定-向上转型-抽象类】
【多态-动态绑定-向上转型-抽象类】
27 0
|
7月前
|
安全 Java 编译器
C++的向上转型
在 C/C++ 中经常会发生数据类型的转换,例如将 int 类型的数据赋值给 float 类型的变量时,编译器会先把 int 类型的数据转换为 float 类型再赋值;反过来,float 类型的数据在经过类型转换后也可以赋值给 int 类型的变量。 数据类型转换的前提是,编译器知道如何对数据进行取舍。例如: int a = 10.9; printf("%d\n", a); 输出结果为 10,编译器会将小数部分直接丢掉(不是四舍五入)。再如: float b = 10; printf("%f\n", b); 输出结果为 10.000000,编译器会自动添
42 0
|
9月前
|
程序员
为什么子类会调用父类无参的构造函数
为什么子类会调用父类无参的构造函数
|
10月前
|
安全
对象的向上转型/向下转型笔记
对象的向上转型/向下转型笔记
|
10月前
|
编译器
Super关键字详解
Super关键字详解
50 0
|
10月前
|
编译器 定位技术
在父类的构造函数中调用虚函数为什么不能实现多态
在父类的构造函数中调用虚函数为什么不能实现多态
75 0
|
11月前
对象向上转型
对象向上转型
46 0