java 铸造扩展相同原始类的子类

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3798663/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 03:25:32  来源:igfitidea点击:

Casting subclasses extending the same original class

javacastingsubclass

提问by MUH Mobile Inc.

How can I cast two extends class like that in java?

我如何在java中投射两个扩展类?

    class B extends Object{

    }

    class C extends Object{

    }

    B b = new B();

    C c = (C)b;//Cannot cast from B to C

回答by Thorbj?rn Ravn Andersen

You can't. Consider a slight rename:

你不能。考虑稍微重命名:

class Ford extends Car {

}
class Chevrolet extends Car {

}

Ford ford = new Ford();

Chevrolet chevrolet = (Chevrolet) ford;

Both are, however, a Car so you can say

然而,两者都是汽车,所以你可以说

Car car = ford;

but not any more than that.

但仅此而已。

回答by hvgotcodes

The closest you can come is to use interfaces

最接近的就是使用接口

 class B extends Object implements Thing {}
 class C extends Object implements Thing {} 

 B b = new B()
 Thing c = (Thing)b

As others have indicated you cannot do what you are trying with just classes.

正如其他人所指出的那样,你不能只用课程来做你正在尝试的事情。

回答by NullUserException

You can't cast an object Bto C, because Bis not a C, the best you can infer is that it's an Object. Here's another analogy:

您不能将对象强制转换BC,因为B它不是C,您可以推断出的最好方法是它是Object。这是另一个类比:

class Animal {
    public void eat();
}

class Dog extends Animal {
    public void bark();
}

public Cat extends Animal {
    public void meow();
}

Say you have:

说你有:

Cat sprinkles = new Cat();

// this doesn't make sense
Dog aDog = (Dog) sprinkles;
aDog.bark(); // can't do this because sprinkles can't bark()

// but this does
Animal myCat = (Animal) sprinkles;
myCat.eat(); // but it can eat()

回答by Jcs

You cannot do that since bis not an instance of class C. It is only possible to cast an object into a super-class or a super-interface.

您不能这样做,因为b它不是类 C 的实例。只能将对象转换为超类或超接口。

回答by Kirk Woll

You categorically cannot do that. You can only cast if the type to which you are casting actually represents the ancestry of the target. In this case, Bsimply is not an instance of C(and vice versa). The real question is why would you want to? And what are you actually trying to accomplish?

你绝对不能这样做。仅当您所投射到的类型实际上代表目标的血统时,您才能施放。在这种情况下,B根本就不是一个实例C(反之亦然)。真正的问题是你为什么想要?你真正想要完成什么?

回答by coobird

In the given code, class Band class Care not in a "is-a" relationship, so they cannot be casted to each other.

在给定的代码中,classB和 classC没有“is-a”关系,因此它们不能相互转换。

The only thing that Band Chas in common is that they are both subclasses of Object, therefore, they can both be casted to Object. In otherwords, Bis-a Objectand Cis-a Object:

那唯一BC拥有的共同点是,它们是两个子类Object,因此,它们都可以被强制转换为Object。换句话说,Bis-aObjectCis-a Object

B b = new B();
Object ob = (Object)b;

C c = new C();
Object oc = (Object)c;


As a counterexample to what is being done, imagine this case:

作为正在做的事情的反例,想象一下这种情况:

class B extends Object {
    public void doSomething();
}

class C extends Object {
    public void doAnotherThing();
}

In this case, what is the following code supposed to do?

在这种情况下,下面的代码应该做什么?

C realC = new C();
realC.doSomething();   // This is OK.

B c = (B)realC;
c.doSomething();       // ???

Since the object made from class Cdoesn't have a doSomethingmethod, what would it do? It can be seen that just because Band Chave a common ancestor does not mean that they can be interchangeable with each other.

既然由类创建的对象C没有doSomething方法,它会做什么?可见,仅仅因为BC具有共同的祖先并不意味着它们可以相互互换。

Therefore, what is attempted in the code above cannot be performed.

因此,无法执行上面代码中尝试的操作。

回答by silvia Dominguez

but...giving this hierarchy class X {}class Z extends X{}class Y extends X{}X z = new Z();X y = new Y();Z y2 =(Z)y;Why is alloweb by the compiler the casting between Z and Y? It not works at runtime obviously cos Z is not a Y.

但是...给出这个层次结构 class X {}class Z extends X{}class Y extends X{}X z = new Z();X y = new Y();Z y2 =(Z)y;为什么编译器允许在 Z 和 Y 之间进行转换?它在运行时显然不起作用,因为 Z 不是 Y。