简体   繁体   English

超类和子类互换?

[英]Superclass and Subclasses Interchange?

I understand that 我明白那个

Superclass object = new subclass(); 

works and makes sense, but does 可行且有意义,但确实

Subclass object = new superclass(); 

work too. 也工作 I'm guessing it wouldn't since you cannot be sure that the superclass "shares the same properties" as the subclass or am I just completely off? 我猜是不会的,因为您不能确定超类是否与子类“共享相同的属性”,或者我只是完全离开了? Thanks for the help! 谢谢您的帮助!

Vehicle x=new Car();

so Car is a type of Vehicle. 所以汽车是车辆的一种

Car x=new Vehicle();  // compilation error here

Vehicle is a type of Car? 车辆是汽车的一种? Does that make any sense? 这有任何意义吗? NO 没有

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM