java学习笔记5:对象继承upcasting
啥也不说了,看代码:
public class Instrument {
protected void play()
{
System.out.println("Instrument playing");
}
static void tune(Instrument i)
{
i.play();
}
}
public class Wind extends Instrument {
protected void play()
{
System.out.println("Wind Playing");
}
public static void main(String[] args)
{
Wind flute=new Wind();
Instrument.tune(flute);
}
}
protected void play()
{
System.out.println("Instrument playing");
}
static void tune(Instrument i)
{
i.play();
}
}
public class Wind extends Instrument {
protected void play()
{
System.out.println("Wind Playing");
}
public static void main(String[] args)
{
Wind flute=new Wind();
Instrument.tune(flute);
}
}
结果会是什么呢,flute被传回父类的时候,是否使用父类的方法呢,看结果:
结果