public class FruitTest {
public static void main(String[] args) {
Fruit fruit = new GoldenDelicious();
Orange orange = new Orange();
//Orange p = new Apple();
//Macintosh p = new Apple();
//Apple p = new Macintosh();
System.out.println(fruit instanceof Orange);
System.out.println(fruit instanceof Apple);
System.out.println(fruit instanceof GoldenDelicious);
System.out.println(fruit instanceof Macintosh);
System.out.println(fruit instanceof Orange);
System.out.println(orange instanceof Fruit);
System.out.println(orange instanceof Apple); //这一句一直是说有问题不匹配(难道不可以输出false吗),但是如果我注释掉,前面ruit fruit = new GoldenDelicious();就报错,但是跟这一句没关系吧,这一句是自动向上转型吗?? 谢谢指导。
}
class GoldenDelicious extends Apple{
public GoldenDelicious(){}
}
class Macintosh extends Apple{
public Macintosh(){}
}
class Apple extends Fruit{
public Apple(){}
// public void makeAppleCider(){}
}
class Orange extends Fruit{
public Orange(){}
//public void makeAppleCider(){}
}
class Fruit{
public Fruit(){}
}
}