如何向下转换 Java 对象?
我正在尝试了解Java的多态性,并且有一个关于对象向下转型的问题。 假设在这个例子中,我有两个子类 Dog 和 Cat,它们继承自超类 Animal
根据我的理解,向下转换对象的唯一方法是该对象已经是良好类型,如下所示:
Animal a = new Dog();
Dog d = (Dog) a;
这有效吗?
但是,如果我想在不知道它是什么的情况下创建一个普通动物,然后在我知道的情况下铸造它,我该怎么做呢?
Animal a = new Animal();
Dog d = (Dog) a;
这会在运行时抛出 ClassCastException 对吗?
我发现做到这一点的唯一方法是创建一个新的 Dog 构造函数,该构造函数从普通动物创建一只狗:
Animal a = new Animal();
Dog d = new Dog(a);
所以
public Class Dog extends Animal{
public Dog(Animal a){
super(a);
}
}
我的问题是,我应该怎么做?
- 我这样做是最好的吗?
- 我是不是根本不应该这样做,如果我必须这样做,就意味着我的程序构思不好?
- 我错过了还有更好的方法吗?
多谢! 恩巴莱耶
I am trying to understand Java's polymorphism, and I have one question about downcasting an object.
Let's say for this example I have two subclasses Dog and Cat that inherit from a superclass Animal
From what I understood, the only way to downcast an object is if this Object is already of the good type, like this:
Animal a = new Dog();
Dog d = (Dog) a;
This works right?
But what if I want to create a regular animal without knowing what it would be, and then cast it when I know, how can I do that?
Animal a = new Animal();
Dog d = (Dog) a;
This will throw a ClassCastException at runtime right?
The only way I found to do that is to create a new Dog constructor that creates a dog from a regular animal:
Animal a = new Animal();
Dog d = new Dog(a);
with
public Class Dog extends Animal{
public Dog(Animal a){
super(a);
}
}
So my question is, how am I supposed to do this?
- Am I doing it the best way?
- Am I not supposed to do this at all, if I have to it means my program is not well conceived?
- Is there a better way I missed?
Thanks a lot!
nbarraille
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果要创建可能因非本地条件而异的类型实例,请使用抽象工厂< /a> (如设计模式书中所述)。
最简单的形式:
还要注意引用的静态类型和对象的动态类型之间存在差异。即使您有
Animal
引用,如果原始对象是Dog
,它的行为仍然类似于Dog
。If you want to create an instance of a type that may vary depending upon non-local conditions, use an Abstract Factory (as described in the Design Patterns book).
In it's simplest form:
Note also there is a difference between the static type of a reference and the dynamic type of the object. Even though you have an
Animal
reference, if the original object is aDog
, it still behaves like aDog
.您应该只转换为对象真正所属的类,因此如果您有一个扩展
Animal
的Dog
,您可以将其转换为Animal
(因为它是一个)但您不应该将Animal
转换为Dog
,因为并非所有Animal
都是Dog
代码>s。Dog
类很可能具有Animal
类未实现的额外字段,因此强制转换没有意义(您将这些值初始化为什么?)。You should only cast to a class that the object really is, so if you have a
Dog
that extendsAnimal
you can cast it to anAnimal
(because it is one) but you shouldn't cast anAnimal
to aDog
because not allAnimal
s areDog
s. TheDog
class may well have extra fields that are not implemented by theAnimal
class and so the cast doesn't make sense (what do you initialise those values to?).Java 是一种强类型语言,这意味着您只能将对象转换为其扩展的类型(超类或接口)。
即使您“伪造它”,例如复制所有类方法和字段,您也无法将对象转换为它不扩展的类型。
根据上面的代码,您无法将
Foo
对象强制转换为Bar
或Baz
,尽管类结构似乎暗示了这一点你可以。不涉及继承,因此会抛出ClassCastException
。Java is a strongly typed language, and that means you can only cast an object to a type it extends from (either a superclass or an interface).
Even if you "fake it", e.g. copy all a classes methods and fields, you just can't cast an object to a type it doesn't extend.
Given the above code, you can't cast a
Foo
object to either aBar
or aBaz
, although the class structure seems to imply that you could. There is no inheritance involved, so aClassCastException
is thrown.这里您正在谈论向下转型,因此在这种情况下,始终应使用超类作为引用,并且应由其指向子对象。
这基本上是工厂模式。
Here you are talking about downcasting, so in this scenario always superclass should be used as a reference and child object should be pointed by that.
This usd basically in factory patter.