Java 基础对象多类型变量

发布于 2024-09-15 15:20:00 字数 991 浏览 8 评论 0原文

我确信这对于像 Java 这样以 OOP 为中心的人来说是非常常见的。在java中,有没有一种方法可以使基类型变量接受所有继承的子类型?就像如果我有一样;

class Mammal {...}
class Dog extends Mammal {...}
class Cat extends Mammal {...}

class ABC {
    private Mammal x;

    ABC() {
        this.x = new Dog();
        -or-
        this.x = new Cat();
    }
}

我需要该变量也能够接受任何扩展版本,但不能接受特定的一种扩展版本。

有些方法我知道,但不想用。我可以为每个子类型创建一个属性,然后只使用实际使用的一个属性。制作一个数组并将其推入其中。

还有其他想法或获取“基类”类型变量的方法吗?


好吧,因为我知道在 Java 中使用多态鸭子类型并不是一个好主意,但因为我认为我无法避免它。这是动态使用子类方法来重新分配变量的强制转换版本的唯一方法,即,我收到错误;

Mammal x;
x = new Dog();
System.out.println(x.getClass());
x.breath();
if (x instanceof Dog) {
  x.bark();
} else if (x instanceof Cat) {
  x.meow();
}

说找不到符号,但这有效;

Mammal x;
x = new Dog();
System.out.println(x.getClass());
x.breath();
if (x instanceof Dog) {
  Dog d = (Dog) x;
  d.bark();
} else if (x instanceof Cat) {
  Cat c = (Cat) x;
  c.meow();
}

最后一种是唯一的方法吗?

I'm sure this is incredibly common with as OOP centered as Java is. In java is there a way to make a base type variable that accepts all inherited subtypes? Like if I have;

class Mammal {...}
class Dog extends Mammal {...}
class Cat extends Mammal {...}

class ABC {
    private Mammal x;

    ABC() {
        this.x = new Dog();
        -or-
        this.x = new Cat();
    }
}

I need the variable to be able to accept any extended version too, but not in specific one extended kind.

There are some ways that I know, but don't want to use. I could make an attribute for each subtype, then only have the one attribute actually used. Make an array and shove it in there.

Any other ideas or a way to get a "base class" type variable?


Ok since I know using polymorphic duck typing isn't a great idea in Java, but since I don't think I can avoid it. Is the only way to use subclass methods dynamically to re assign a casted version of the varible ie, I get an error with this;

Mammal x;
x = new Dog();
System.out.println(x.getClass());
x.breath();
if (x instanceof Dog) {
  x.bark();
} else if (x instanceof Cat) {
  x.meow();
}

Saying symbol not found, however this works;

Mammal x;
x = new Dog();
System.out.println(x.getClass());
x.breath();
if (x instanceof Dog) {
  Dog d = (Dog) x;
  d.bark();
} else if (x instanceof Cat) {
  Cat c = (Cat) x;
  c.meow();
}

That last one the only way to do it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

远山浅 2024-09-22 15:20:00

如果您有以下情况:

class Mammal {...}
class Dog extends Mammal {...}
class Cat extends Mammal {...}

那么 DogMammal 的子类型。 也是哺乳动物的一个子类型。这种类型多态性实际上允许您执行以下操作:

Mammal x;
x = new Dog(); // fine!
x = new Cat(); // also fine!

如果事实上稍后有以下内容:

class Platypus extends Mammal {...} // yes it's true!

那么您还可以执行以下操作:

x = new Platypus(); // fine!

这种多态子类型关系是面向对象编程的基本原则之一。

另请参阅


instanceof 类型比较运算符上

假设我们有以下内容

class Mammal { void lactate(); }
class Dog extends Mammal { void bark(); }
class Cat extends Mammal { void meow(); }

:您可以使用 instanceof 类型比较运算符 (§15.20.2) 执行类似的操作:

Mammal x = ...;

if (x instanceof Dog) {
   Dog d = (Dog) x;
   d.bark();
} else if (x instanceof Cat) {
   Cat c = (Cat) x;
   c.meow();
}
if (x != null) {
   x.lactate();
}

也有一些方法可以在不使用 if-else 的情况下执行此操作;这只是一个简单的例子。

请注意,通过适当的设计,您也许能够避免其中一些子类型区分逻辑。例如,如果 Mammal 有一个 makeSomeNoise() 方法,您只需调用 x.makeSomeNoise() 即可。

相关问题


在反射上

如果必须处理编译时未知的新类型,那么您可以诉诸反射。请注意,对于一般应用程序,几乎总是有比反射更好的替代方案。

另请参阅

If you have the following:

class Mammal {...}
class Dog extends Mammal {...}
class Cat extends Mammal {...}

Then Dog is a subtype of Mammal. Cat is also a subtype of Mammal. This type polymorphism does in fact allow you to do the following:

Mammal x;
x = new Dog(); // fine!
x = new Cat(); // also fine!

If in fact later there's the following:

class Platypus extends Mammal {...} // yes it's true!

Then you can also do:

x = new Platypus(); // fine!

This polymorphic subtyping relationship is one of the basic tenets of object-oriented programming.

See also


On instanceof type comparison operator

Suppose we have the following:

class Mammal { void lactate(); }
class Dog extends Mammal { void bark(); }
class Cat extends Mammal { void meow(); }

Then you can use instanceof type comparison operator (§15.20.2) to do something like this:

Mammal x = ...;

if (x instanceof Dog) {
   Dog d = (Dog) x;
   d.bark();
} else if (x instanceof Cat) {
   Cat c = (Cat) x;
   c.meow();
}
if (x != null) {
   x.lactate();
}

There are also ways to do this without if-else; this is just given as a simple example.

Note that with appropriate design, you may be able to avoid some of these kinds of subtype differentiation logic. If Mammal has a makeSomeNoise() method, for example, you can simply call x.makeSomeNoise().

Related questions


On reflection

If you must deal with new types not known at compile-time, then you can resort to reflection. Note that for general applications, there are almost always much better alternatives than reflection.

See also

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