如何向下转换 Java 对象?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4033118/
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
How to downcast a Java object?
提问by nbarraille
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
我试图理解 Java 的多态性,并且我有一个关于向下转换对象的问题。假设在此示例中,我有两个子类 Dog 和 Cat,它们继承自超类 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?
这会在运行时抛出 ClassCastException 对吗?
The only way I found to do that is to create a new Dog constructor that creates a dog from a regular animal:
我发现这样做的唯一方法是创建一个新的 Dog 构造函数,该构造函数从普通动物中创建一条狗:
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
非常感谢!nbarraille
采纳答案by Tom Hawtin - tackline
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:
最简单的形式:
interface AnimalFactory {
Animal createAnimal();
}
class DogFactory implements AnimalFactory {
public Dog createAnimal() {
return new Dog();
}
}
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 a Dog
, it still behaves like a Dog
.
另请注意,引用的静态类型和对象的动态类型之间存在差异。即使你有一个Animal
引用,如果原始对象是 a Dog
,它的行为仍然像 a Dog
。
回答by Sean Patrick Floyd
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).
Java 是一种强类型语言,这意味着您只能将对象强制转换为它所继承的类型(超类或接口)。
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.
即使您“伪造”,例如复制所有类的方法和字段,您也无法将对象强制转换为它不扩展的类型。
public class Foo{
public String phleem;
public void bar(){
}
}
public class Bar{
public String phleem;
}
public interface Baz{
public void bar();
}
Given the above code, you can't cast a Foo
object to either a Bar
or a Baz
, although the class structure seems to imply that you could. There is no inheritance involved, so a ClassCastException
is thrown.
鉴于上述代码,您不能将Foo
对象强制转换为 aBar
或 a Baz
,尽管类结构似乎暗示您可以。不涉及继承,因此ClassCastException
抛出 a 。
回答by Hymanson Pope
You should only cast to a class that the object really is, so if you have a Dog
that extends Animal
you can cast it to an Animal
(because it is one) but you shouldn't cast an Animal
to a Dog
because not all Animal
s are Dog
s. The Dog
class may well have extra fields that are not implemented by the Animal
class and so the cast doesn't make sense (what do you initialise those values to?).
您应该只强制转换为对象真正所在的类,因此如果您有一个Dog
扩展类,Animal
您可以将其强制转换为一个Animal
(因为它是一个),但您不应该将 an 强制转换Animal
为 a,Dog
因为并非所有Animal
s 都是Dog
s。该Dog
班可能有不被执行额外的字段Animal
类等演员没有任何意义(你怎么初始化这些值?)。
回答by Arun Pandeu
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.
在这里,您正在谈论向下转换,因此在这种情况下,应始终将超类用作引用,并且应通过该引用指向子对象。这美元基本上是工厂模式。