如何向下转换 Java 对象?

发布于 2024-09-29 12:41:48 字数 753 浏览 6 评论 0原文

我正在尝试了解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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

南城旧梦 2024-10-06 12:41:48

如果要创建可能因非本地条件而异的类型实例,请使用抽象工厂< /a> (如设计模式书中所述)。

最简单的形式:

interface AnimalFactory {
    Animal createAnimal();
}

class DogFactory implements AnimalFactory {
    public Dog createAnimal() {
        return new Dog();
    }
}

还要注意引用的静态类型和对象的动态类型之间存在差异。即使您有 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:

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.

巨坚强 2024-10-06 12:41:48

您应该只转换为对象真正所属的类,因此如果您有一个扩展 AnimalDog,您可以将其转换为 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 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 Animals are Dogs. 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?).

乖乖公主 2024-10-06 12:41:48

Java 是一种强类型语言,这意味着您只能将对象转换为其扩展的类型(超类或接口)。

即使您“伪造它”,例如复制所有类方法和字段,您也无法将对象转换为它不扩展的类型。

public class Foo{

    public String phleem;

    public void bar(){

    }

}

public class Bar{

    public String phleem;

}

public interface Baz{

    public void bar();

}

根据上面的代码,您无法将 Foo 对象强制转换为 BarBaz,尽管类结构似乎暗示了这一点你可以。不涉及继承,因此会抛出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.

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.

陈年往事 2024-10-06 12:41:48

这里您正在谈论向下转型,因此在这种情况下,始终应使用超类作为引用,并且应由其指向子对象。
这基本上是工厂模式。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文