Car
当我尝试用接口指向类的对象Colorable
而不使用类 car 中的实现时,我没有显示任何编译错误(尽管它显示运行时错误)(问题 1)并且当我尝试对我不扩展的类执行相同操作时车辆类并尝试用车辆类指向汽车对象它立即显示编译错误。为什么会这样?
问题1:
interface Colorable {}
class Vehicle {}
class Car extends Vehicle {}
public class Tester {
public static void main(String[] args) {
Car c=new Car();
Vehicle a = (Vehicle)c;
Colorable i = (Colorable)c;
System.out.println("Successful");
}
}
问题2:
interface Colorable {}
class Vehicle {}
class Car {}
public class Tester {
public static void main(String[] args) {
Car c=new Car();
Vehicle a = (Vehicle)c;
Colorable i = (Colorable)c;
System.out.println("Successful");
}
}