什么是多态性,它的用途是什么,如何使用它?


当前回答

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.

其他回答

我知道这是一个有很多好答案的老问题,但我想用一句话来回答:

将派生类型视为其基类型。

上面有很多例子可以说明这一点,但我觉得这是一个很好的简明答案。

多态是指您可以将对象视为某个对象的通用版本,但当您访问它时,代码将确定它的确切类型并调用相关代码。

下面是一个c#的例子。在控制台应用程序中创建四个类:

public abstract class Vehicle
{
    public abstract int Wheels;
}

public class Bicycle : Vehicle
{
    public override int Wheels()
    {
        return 2;
    }
}

public class Car : Vehicle
{
    public override int Wheels()
    {
        return 4;
    }
}

public class Truck : Vehicle
{
    public override int Wheels()
    {
        return 18;
    }
}

现在在控制台应用程序模块的Main()中创建以下内容:

public void Main()
{
    List<Vehicle> vehicles = new List<Vehicle>();

    vehicles.Add(new Bicycle());
    vehicles.Add(new Car());
    vehicles.Add(new Truck());

    foreach (Vehicle v in vehicles)
    {
        Console.WriteLine(
            string.Format("A {0} has {1} wheels.",
                v.GetType().Name, v.Wheels));
    }
}

在这个例子中,我们创建了一个基类Vehicle的列表,它不知道它的每个子类有多少个轮子,但是知道每个子类负责知道它有多少个轮子。

然后我们将自行车、汽车和卡车添加到列表中。

接下来,我们可以循环遍历列表中的每个Vehicle,并将它们都视为相同的,但是当我们访问每个Vehicles 'Wheels'属性时,Vehicle类将该代码的执行委托给相关的子类。

该代码被称为多态代码,因为执行的确切代码是由在运行时引用的子类决定的。

我希望这对你有所帮助。

多态性一词来自:

形态性=改变的能力

在编程中,多态性是一种“技术”,它允许您将一个对象“看作”不止一种类型的事物。例如:

学生对象也是人对象。如果你“看”这个学生,你可能会问他要学生证。你不能总是对一个人这么做,对吧?(一个人不一定是学生,因此可能没有学生证)。然而,每个人可能都有名字。学生也一样。

底线是,从不同的“角度”“看”同一个对象可以给你不同的“视角”(即不同的属性或方法)

因此,这种技术可以让你构建可以从不同角度“观察”的东西。

为什么我们使用多态性?首先……抽象。在这一点上,它应该是足够的信息:)

简单类比解释

美国的总统采用多态。怎么做?他有很多顾问:

军事顾问 法律顾问 核物理学家(作为顾问) 等等。

每个人只应该对一件事负责:

总统不是镀锌层专家,也不是量子物理专家。他不知道很多事情。但他知道如何管理国家。

代码也是一样:关注点和职责应该被分离到相关的类/人。这使得维护代码更加容易,特别是当您正在进行更改时。变化是不可避免的。当事情发生变化时,您不希望破坏应用程序的其他部分。总统们应该专注于管理国家,而不是进入专业领域的细节:

为什么一个总统知道所有这些具体的事情是一个坏主意?

如果总统明确地告诉人们要做什么,那就意味着总统需要确切地知道要做什么。如果总统自己需要知道具体的事情,这意味着当你需要做出改变时,你需要在两个地方做出改变,而不仅仅是一个地方。

例如,如果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))

我真的希望它能帮到你。如果你不明白任何张贴评论,我会再试一次。

一般来说,它是一种使用相同或表面上相似的API来为许多不同类型的对象提供接口的能力。有多种形式:

Function overloading: defining multiple functions with the same name and different parameter types, such as sqrt(float), sqrt(double) and sqrt(complex). In most languages that allow this, the compiler will automatically select the correct one for the type of argument being passed into it, thus this is compile-time polymorphism. Virtual methods in OOP: a method of a class can have various implementations tailored to the specifics of its subclasses; each of these is said to override the implementation given in the base class. Given an object that may be of the base class or any of its subclasses, the correct implementation is selected on the fly, thus this is run-time polymorphism. Templates: a feature of some OO languages whereby a function, class, etc. can be parameterised by a type. For example, you can define a generic "list" template class, and then instantiate it as "list of integers", "list of strings", maybe even "list of lists of strings" or the like. Generally, you write the code once for a data structure of arbitrary element type, and the compiler generates versions of it for the various element types.