java 将子类对象分配给超类实例类型只是为了覆盖概念?或者我们也在为其他东西这样做?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16188122/
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
Assigning subclass object to Superclass instance type is only for overriding concept?or we are doing this for something else also?
提问by Rose
Do you assign a subclass object to a Superclass instance type only for overriding? Or are we doing this for something else also?
您是否将子类对象分配给超类实例类型仅用于覆盖?或者我们是否也在为别的事情做这件事?
Example:
Sub is subtype of Super and
Super s = new Sub();
示例:Sub 是 Super 的子类型,并且
Super s = new Sub();
Please explain.
请解释。
回答by MD Sayem Ahmed
When someone writes code like this, he/she is trying to follow a basic OO design principle which says -
当有人编写这样的代码时,他/她试图遵循基本的面向对象设计原则,即 -
Program to an interface, not to a concrete implementation
编程到接口,而不是具体的实现
I have explained this principle in one of my blog posts. Look in the Class Inheritance VS Interface Inheritance
section.
我在我的一篇博文中解释了这个原则。看Class Inheritance VS Interface Inheritance
栏目。
To summarize the post, when you use a reference of a parent type to refer to an instance of a sub-type, you get a lot of flexibility. For example, if you ever need to change your sub-type implementation in the future, you will be able to do that easily, without changing much of your code.
总结这篇文章,当您使用父类型的引用来引用子类型的实例时,您将获得很大的灵活性。例如,如果您将来需要更改您的子类型实现,您将能够轻松地做到这一点,而无需更改您的大部分代码。
Consider the following method -
考虑以下方法 -
public void DoSomeStuff(Super s) {
s.someMethod();
}
and a call to this method -
并调用此方法 -
DoSomeStuff(new Sub());
now, if you ever need to change the logic inside someMethod
, you can easily do it by declaring a new subtype of Super
, say NewSubType
, and changing the logic inside that implementation. In this way, you will never have to touch other existing code which utilizes that method. You will still be able to use your DoSomeStuff
method in the following way -
现在,如果您需要更改 内部的逻辑someMethod
,您可以通过声明一个新的子类型Super
,例如NewSubType
,并更改该实现内部的逻辑来轻松实现。通过这种方式,您将永远不必接触使用该方法的其他现有代码。您仍然可以DoSomeStuff
通过以下方式使用您的方法 -
DoSomeStuff(new NewSubType());
Had you declared the parameter of DoSomeStuff
to be of Sub
, you would then have to change its implementation too -
如果您将参数声明DoSomeStuff
为 of Sub
,那么您也必须更改其实现 -
DoSomeStuff(NewSubType s) {
s.someMethod();
}
and it may also chain/bubble to several other places.
它也可能链接/冒泡到其他几个地方。
回答by kutschkem
Yes, but "overriding" is not the term i would use. Often, the reason to have a class hierarchy is not to alterthe behavior of the super class, but to implementit. More often than not, the super class will either be an interface, and not a class, or be an abstract class following the template method pattern, where some methods are kept abstract and multiple subclasses implement those, but the general behavior is already fixed.
是的,但“覆盖”不是我会使用的术语。通常,拥有类层次结构的原因不是为了改变超类的行为,而是为了实现它。通常情况下,超类要么是一个接口,而不是一个类,要么是一个遵循模板方法模式的抽象类,其中一些方法保持抽象,多个子类实现这些,但一般行为已经固定。
More concretely though, if we assign a subclass to a superclass instance variable like in your example, we want to limit the impact on our code in the case we exchange the subclass (we only need to edit one single line!)
更具体地说,如果我们像在您的示例中那样将子类分配给超类实例变量,我们希望在交换子类的情况下限制对代码的影响(我们只需要编辑一行!)
回答by david a.
The characteristics of OO design that allows this (to handle object as instances of a common supertype) is called polymorphism.
允许这样做(将对象作为公共超类型的实例处理)的 OO 设计的特征称为多态性。
One would assign a sub-type instance of to a supertype variable to handle all possible subtype classes in a uniform fashion, e.g. using methods declared (but possibly overriden) by the supertype class.
可以将 的子类型实例分配给超类型变量,以统一方式处理所有可能的子类型类,例如使用由超类型类声明(但可能覆盖)的方法。
Related OO topics you might find interesting to read about:
您可能会感兴趣阅读的相关 OO 主题: