什么是多态性,它的用途是什么,如何使用它?
当前回答
在编码术语中,多态性是指您的对象可以通过继承等方式以多种类型存在。如果你创建了一个名为“Shape”的类,它定义了对象的边数,那么你可以创建一个继承它的新类,如“Square”。当你随后创建一个“Square”实例时,你可以根据需要将它从“Shape”前后转换为“Square”。
其他回答
通常这指的是A类型对象的行为与b类型对象相似的能力。在面向对象编程中,这通常是通过继承来实现的。一些维基百科的链接来阅读更多:
面向对象编程中的多态性 类型多态性
编辑:固定破碎的链接。
多态性一词来自:
多
形态性=改变的能力
在编程中,多态性是一种“技术”,它允许您将一个对象“看作”不止一种类型的事物。例如:
学生对象也是人对象。如果你“看”这个学生,你可能会问他要学生证。你不能总是对一个人这么做,对吧?(一个人不一定是学生,因此可能没有学生证)。然而,每个人可能都有名字。学生也一样。
底线是,从不同的“角度”“看”同一个对象可以给你不同的“视角”(即不同的属性或方法)
因此,这种技术可以让你构建可以从不同角度“观察”的东西。
为什么我们使用多态性?首先……抽象。在这一点上,它应该是足够的信息:)
OOP中的多态性意味着一个类可以有不同的类型,继承是实现多态性的一种方式。
for example, Shape is an interface, it has Square, Circle, Diamond subtypes. now you have a Square object, you can upcasting Square to Shape automatically, because Square is a Shape. But when you try to downcasting Shape to Square, you must do explicit type casting, because you can't say Shape is Square, it could be Circle as well. so you need manually cast it with code like Square s = (Square)shape, what if the shape is Circle, you will get java.lang.ClassCastException, because Circle is not Square.
多态:
根据类的实例而不是引用变量的类型执行不同的执行。
接口类型引用变量可以引用实现该接口的任何类实例。
简单类比解释
美国的总统采用多态。怎么做?他有很多顾问:
军事顾问 法律顾问 核物理学家(作为顾问) 等等。
每个人只应该对一件事负责:
总统不是镀锌层专家,也不是量子物理专家。他不知道很多事情。但他知道如何管理国家。
代码也是一样:关注点和职责应该被分离到相关的类/人。这使得维护代码更加容易,特别是当您正在进行更改时。变化是不可避免的。当事情发生变化时,您不希望破坏应用程序的其他部分。总统们应该专注于管理国家,而不是进入专业领域的细节:
为什么一个总统知道所有这些具体的事情是一个坏主意?
如果总统明确地告诉人们要做什么,那就意味着总统需要确切地知道要做什么。如果总统自己需要知道具体的事情,这意味着当你需要做出改变时,你需要在两个地方做出改变,而不仅仅是一个地方。
例如,如果EPA改变了污染法律,那么当这种情况发生时:你必须对EPA类和总统类进行更改。在两个地方而不是一个地方更改代码可能是危险的——因为它更难维护。
有没有更好的方法?
有一个更好的方法:总统不需要知道任何事情的细节——他可以从专门负责这些事情的人那里得到最好的建议。
他可以用多态的方法来治理国家。
使用多态方法的例子:
总统所做的就是让人们给他提建议——这就是他在现实生活中所做的——这就是一个好总统应该做的。他的顾问们都有不同的反应,但他们都知道总统是什么意思:Advise()。成百上千的人涌进他的办公室。他们是谁并不重要。总统所知道的是,当他要求他们“建议”时,他们知道如何相应回应:
public class MisterPresident
{
public void RunTheCountry()
{
// assume the Petraeus and Condi classes etc are instantiated.
petraeus.Advise(); // # Petraeus says send 100,000 troops to Fallujah
condolezza.Advise(); // # she says negotiate trade deal with Iran
healthOfficials.Advise(); // # they say we need to spend $50 billion on ObamaCare
}
}
这种方法允许总统在不了解任何军事、医疗或国际外交的情况下管理国家:细节留给专家。总统需要知道的唯一一件事是:“Advise()”。
你不想要的:
public class MisterPresident
{
public void RunTheCountry()
{
// people walk into the Presidents office and he tells them what to do
// depending on who they are.
// Fallujah Advice - Mr Prez tells his military exactly what to do.
petraeus.IncreaseTroopNumbers();
petraeus.ImproveSecurity();
petraeus.PayContractors();
// Condi diplomacy advice - Prez tells Condi how to negotiate
condi.StallNegotiations();
condi.LowBallFigure();
condi.FireDemocraticallyElectedIraqiLeaderBecauseIDontLikeHim();
// Health care
healthOfficial.IncreasePremiums();
healthOfficial.AddPreexistingConditions();
}
}
NO! NO! NO! In the above scenario, the president is doing all the work: he knows about increasing troop numbers and pre-existing conditions. This means that if middle eastern policies change, then the president would have to change his commands, as well as the Petraeus class as well. We should only have to change the Petraeus class, because the President shouldn't have to get bogged down in that sort of detail. He doesn't need to know about the details. All he needs to know is that if he makes one order, everything will be taken care of. All the details should be left to the experts.
这使得总统可以做他最擅长的事情:制定总体政策,保持良好的形象,打高尔夫球。
它实际上是如何实现的——通过基类还是公共接口
简而言之,这实际上就是多态性。具体是怎么做到的呢?通过“实现一个公共接口”或者通过使用基类(继承)——参见上面的回答,它们更详细地说明了这一点。(为了更清楚地理解这个概念,您需要知道接口是什么,还需要了解继承是什么。否则,你可能会很挣扎。)
换句话说,Petraeus、Condi和healthoofficial都是“实现接口”的类——让我们称其为IAdvisor接口,它只包含一个方法:Advise()。但现在我们进入细节。
这是最理想的
public class MisterPresident
{
// You can pass in any advisor: Condi, HealthOfficials,
// Petraeus etc. The president has no idea who it will
// be. But he does know that he can ask them to "advise"
// and that's all Mr Prez cares for.
public void RunTheCountry(IAdvisor governmentOfficer)
{
governmentOfficer.Advise();
}
}
public class USA
{
MisterPresident president;
public USA(MisterPresident president)
{
this.president = president;
}
public void ImplementPolicy()
{
IAdvisor governmentOfficer = getAdvisor(); // Returns an advisor: could be condi, or petraus etc.
president.RunTheCountry(governmentOfficer);
}
}
总结
你真正需要知道的是:
总统不需要知道细节——那些是留给别人的。 总统所需要知道的是问谁曾经走进家门给他建议-我们知道他们绝对知道当被要求建议时该做什么(因为他们实际上都是顾问(或IAdvisors))
我真的希望它能帮到你。如果你不明白任何张贴评论,我会再试一次。