Java 基础对象多类型变量
我确信这对于像 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您有以下情况:
那么
Dog
是Mammal
的子类型。猫
也是哺乳动物
的一个子类型。这种类型多态性实际上允许您执行以下操作:如果事实上稍后有以下内容:
那么您还可以执行以下操作:
这种多态子类型关系是面向对象编程的基本原则之一。
另请参阅
<块引用>
子类型多态性,在面向对象编程的上下文中几乎普遍称为多态性,是一种类型A作为另一种类型B
出现和使用的能力
在
instanceof
类型比较运算符上假设我们有以下内容
:您可以使用
instanceof
类型比较运算符 (§15.20.2) 执行类似的操作:也有一些方法可以在不使用
if-else
的情况下执行此操作;这只是一个简单的例子。请注意,通过适当的设计,您也许能够避免其中一些子类型区分逻辑。例如,如果
Mammal
有一个makeSomeNoise()
方法,您只需调用x.makeSomeNoise()
即可。相关问题
在反射上
如果必须处理编译时未知的新类型,那么您可以诉诸反射。请注意,对于一般应用程序,几乎总是有比反射更好的替代方案。
另请参阅
If you have the following:
Then
Dog
is a subtype ofMammal
.Cat
is also a subtype ofMammal
. This type polymorphism does in fact allow you to do the following:If in fact later there's the following:
Then you can also do:
This polymorphic subtyping relationship is one of the basic tenets of object-oriented programming.
See also
On
instanceof
type comparison operatorSuppose we have the following:
Then you can use
instanceof
type comparison operator (§15.20.2) to do something like this: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 amakeSomeNoise()
method, for example, you can simply callx.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