简体   繁体   English

当超类没有实现接口而是子类时,在接口子类和超类之间进行转换

[英]Casting between interface sublass and superclass when superclass is not implementing interface but subclass is

In the following code 在以下代码中

abstract class Vehicle { }
class Car extends Vehicle implements LandMover { }
interface LandMover { }
     Car porsche=new Car();
     LandMover lmv;
     lmv = porsche;
     Vehicle vec = (Vehicle)lmv;

Shouldnt there be a compiler error in 4th line as there is no relation between class vehicle and Interface LandMover?? 因为类车和接口LandMover之间没有关系,所以第4行不应该有编译错误吗? and if there isnt what could be the reason.Thanks!! 如果没有什么可能是原因。谢谢!!

The compiler only checks if there is a possible relationship, and there is one: 编译器只检查是否存在可能的关系,并且只有一个:

A LandMover could be a Car , which in turn IS-A Vehicle . LandMover可以是一辆Car ,而后者又是一辆Vehicle Since you promise that this conversion is ok by using an explicit cast, the compiler is happy. 由于您通过使用显式强制转换来保证此转换正常,因此编译器很高兴。

No, since 不,从那以后

    LandMover lmv = porsche;

doesn't declare an object of type LandMover, but refers to an object of type 'Car' via reference 'LandMover'. 不声明LandMover类型的对象,而是通过引用'LandMover'引用类型为'Car'的对象。 Compiler knows that this is an object of type 'Car' (note: 'Car' and "Vehicale' are in the same inheritance tree). 编译器知道这是'Car'类型的对象(注意:'Car'和'Vehicale'在同一个继承树中)。

By explicitly casting to Vehicle , the compiler will not tell you anything. 通过显式转换为Vehicle ,编译器不会告诉您任何内容。 You might just get an Exception at runtime if the relationships finally don't quite work out and you are casting something that is actually not a Vehicle 你可能只是在运行时得到一个Exception,如果关系最终没有完全解决并且你正在构建一些实际上不是Vehicle

Compilation will be fine with the code you have provided. 使用您提供的代码编译即可。 lmv = porsche; is same as saying 和说的一样

public class Car extends Vehicle implements LandMover {

    public static void main(String args[]){

        LandMover car = new Car();
    }
}

A Car implements LandMover so you can use it as an polymorphic reference. Car实现了LandMover,因此您可以将其用作多态参考。 So no problem so far. 所以到目前为止没问题。

Vehicle vec = (Vehicle)lmv;

Now you are saying the LandMover(reference) which can very well be a Car is fact a Vehicle. 现在你说LandMover(参考)很可能是一辆Car实际上是一辆车。 So compiler will not complaint. 所以编译器不会投诉。

But note after this type cast if you try to call some function on vec that is not defined in Vehicle Class(Not even the ones in Car class though infact it is an runtime instance of Car class) compiler will complaint. 但是注意这种类型转换之后如果你试图调用未在Vehicle类中定义的vec上的某些函数(甚至不是Car类中的那些函数,尽管它实际上是Car类的运行时实例)编译器会抱怨。

This is because at runtime compiler only checks the class of reference type and if the function that is called is defined(may not be implemented) in it then there will not be any compilation errors. 这是因为在运行时编译器只检查引用类的类,如果在其中定义(可能没有实现)被调用的函数,则不会有任何编译错误。

There won't be any compile errors. 不会有任何编译错误。 This is because a class can implement many interfaces therefore a given 'LandMover' instance can be a Vehicle. 这是因为类可以实现许多接口,因此给定的“LandMover”实例可以是Vehicle。 However since a class can inherit from a single super class you know that you can't cast a 'java.lang.String' to 'Vehicle' because they don't share a common superclass other than object. 但是,由于类可以从单个超类继承,因此您知道不能将'java.lang.String'转换为'Vehicle',因为它们不共享除object之外的公共超类。

Considering the hierarchy 考虑到层次结构

Car extends Vehicle

&

Car implements LandMover

For the casting Vehicle vec = (Vehicle)lmv; 对于铸造Vehicle vec = (Vehicle)lmv; the compiler can check statically (compile time), the type of lmv which is LandMover and which in turn also CAN be Car (as Car implements LandMover ). 编译器可以静态地检查(编译时间)的类型, lmvLandMover和这反过来也可以Car (作为Car implements LandMover )。 Now as Car extends Vehicle the compiler does not complain. 现在随着Car extends Vehicle ,编译器不会抱怨。

to Confirm it, try commenting the code as follows 要确认 ,请尝试按如下方式注释代码

        //Car porsche=new Car();
         LandMover lmv;
         //lmv= porsche;
         Vehicle vec = (Vehicle)lmv; //Compiler error

Here, compiler is sure that lmv CANNOT be Vehicle for sure and hence flags error. 在这里,编译器确定lmv肯定不是 Vehicle ,因此标志错误。

由于抽象类无法实例化以便更多地引用此帖子,因此会出现错误

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

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