为什么维基百科说“多态性与方法重载或方法重写不同。”

发布于 2024-09-11 23:48:50 字数 355 浏览 4 评论 0原文

我环顾四周,找不到任何类似的问题。

这是我从 Wikipedia 得到的段落:

多态性是不一样的作为方法重载或方法覆盖。多态性只涉及接口或更通用基类的特定实现的应用。方法重载是指同一个类中具有相同名称但不同签名的方法。方法重写是指子类替换其父类的一个或多个方法的实现。方法重载和方法重写本身都不是多态性的实现。

这里有人可以更清楚地解释一下吗,特别是“多态性与方法重写不同”部分?我现在很困惑。提前致谢。

I have looked around and could not find any similar question.

Here is the paragraph I got from Wikipedia:

Polymorphism is not the same as method overloading or method overriding. Polymorphism is only concerned with the application of specific implementations to an interface or a more generic base class. Method overloading refers to methods that have the same name but different signatures inside the same class. Method overriding is where a subclass replaces the implementation of one or more of its parent's methods. Neither method overloading nor method overriding are by themselves implementations of polymorphism.

Could anyone here explain it more clearly, especially the part "Polymorphism is not the same as method overriding"? I am confused now. Thanks in advance.

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

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

发布评论

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

评论(7

绾颜 2024-09-18 23:48:50

多态性(非常简单地说)是在需要基类的情况下使用派生类的可能性:

class Base {

}

class Derived extends Base  {

}

Base v = new Derived(); // OK

另一方面,方法重写是 Wiki 所说的一种更改派生类中方法行为的方法:

class Shape  {
  void draw() { /* Nothing here, could be abstract*/ }
}

class Square extends Shape  {
  @Override
  void draw() { /* Draw the square here */ }
}

重载与继承无关,它允许定义更多具有相同名称的函数,这些函数仅在它们所采用的参数上有所不同。

Polymorphism (very simply said) is a possibility to use a derived class where a base class is expected:

class Base {

}

class Derived extends Base  {

}

Base v = new Derived(); // OK

Method overriding, on the other hand, is as Wiki says a way to change the method behavior in a derived class:

class Shape  {
  void draw() { /* Nothing here, could be abstract*/ }
}

class Square extends Shape  {
  @Override
  void draw() { /* Draw the square here */ }
}

Overloading is unrelated to inheritance, it allows defining more functions with the same name that differ only in the arguments they take.

清风不识月 2024-09-18 23:48:50

您可以在不允许方法重写(甚至继承)的语言中拥有多态性。例如,让几个不同的对象实现相同的接口。多态性只是意味着同一个抽象接口可以有不同的具体实现。有些语言不鼓励或不允许继承,但本着抽象编程的精神允许这种多态性。

理论上,您还可以在不允许虚拟方法分派的语言中进行方法重写,而无需多态性。其效果是,您可以创建一个具有重写方法的新类,但无法使用它来代替父类。我不知道有任何主流语言可以做到这一点。

You can have polymorphism in a language that does not allow method overriding (or even inheritance). e.g. by having several different objects implement the same interface. Polymorphism just means that you can have different concrete implementations of the same abstract interface. Some languages discourage or disallow inheritance but allow this kind of polymorphism in the spirit of programming with abstractions.

You could also theoretically have method overriding without polymorphism in a language that doesn't allow virtual method dispatch. The effect would be that you could create a new class with overridden methods, but you wouldn't be able to use it in place of the parent class. I'm not aware of any mainstream language that does this.

人间☆小暴躁 2024-09-18 23:48:50

多态性并不是方法被覆盖;而是方法被覆盖。它是关于确定特定流程的实现的对象。一个简单的例子 - 但绝不是唯一的例子 - 是继承:

小说是书籍的一种类型。它具有大部分相同的方法,并且您可以对书籍进行的所有操作也可以对小说进行。因此,任何接受 Book 作为参数的方法也可以处理 Novel 作为参数。 (示例包括 .read()、.write()、.burn())。这本身并不是指小说可以覆盖 Book 方法的事实。相反,它指的是抽象的一个特征。如果教授指定要阅读一本书,他/她并不关心你如何阅读它 - 只要你关心。同样,调用程序并不关心 Book 类型的对象如何被读取,只关心它本身就是这样。如果对象是小说,它将被作为小说阅读。如果它不是小说但仍然是一本书,那么它将被当作一本书来阅读。

书籍:

private void read(){

#Read the book.
}

小说:

private void read(){

#Read a book, and complain about how long it is, because it's a novel!

}

重载方法只是指具有相同名称但参数数量不同的两个方法。例子:

writeNovel(int numPages, String name)

writeNovel(String name)

Polymorphism is not about methods being overridden; it is about the objects determining the implementation of a particular process. An easy example - but by no means the only example - is with inheritance:

A Novel is a type of Book. It has most of the same methods, and everything you can do to a Book can also be done to a Novel. Therefore, any method that accepts a Book as an argument can also deal with a Novel as an argument. (Example would include .read(), .write(), .burn()). This is, per se, not referring to the fact that a Novel can overwrite a Book method. Instead, it is referring to a feature of abstraction. If a professor assigns a Book to be read, he/she doesn't care how you read it - just that you do. Similarly, a calling program doesn't care how an object of type Book is read, just that it is. If the object is a Novel, it will be read as a Novel. If it is not a novel but is still a book, it will be read as a Book.

Book:

private void read(){

#Read the book.
}

Novel:

private void read(){

#Read a book, and complain about how long it is, because it's a novel!

}

Overloading methods is just referring to having two methods with the same name but a different number of arguments. Example:

writeNovel(int numPages, String name)

writeNovel(String name)
晨曦慕雪 2024-09-18 23:48:50

重载是在同一个类中具有许多名称相同但参数不同的方法。

重写是在继承类中具有与基类相同的方法+参数。因此,根据对象的类,将调用基方法或继承的方法。

多态性是指,当作为参数给出时,继承类的实例可以替换基类的实例。

例如:

class Shape {
  public void draw() {
    //code here
  }
  public void draw(int size) {
    //this is overloading 
  }
}

class Square inherits Shape {
  public void draw() {
    //some other code : this is overriding
  }

  public void draw(color c) {
    //this is overloading too
  }
}

class Work {
  public myMethod(Shape s) {
    //using polymophism, you can give to this method
    //a Shape, but also a Square, because Square inherits Shape.
  }
}

看到了吗?
多态是指同一个对象可以用作其自己的类、其基类的实例,甚至可以用作接口。

Overloading is having, in the same class, many methods with the same name, but differents parameters.

Overriding is having, in an inherited class, the same method+parameters of a base class. Thus, depending on the class of the object, either the base method, or the inherited method will be called.

Polymorphism is the fact that, an instance of an inherited class can replace an instance of a base class, when given as a parameters.

E.g. :

class Shape {
  public void draw() {
    //code here
  }
  public void draw(int size) {
    //this is overloading 
  }
}

class Square inherits Shape {
  public void draw() {
    //some other code : this is overriding
  }

  public void draw(color c) {
    //this is overloading too
  }
}

class Work {
  public myMethod(Shape s) {
    //using polymophism, you can give to this method
    //a Shape, but also a Square, because Square inherits Shape.
  }
}

See it ?
Polymorphing is the fact that, the same object, can be used as an instance of its own class, its base class, or even as an interface.

黯然 2024-09-18 23:48:50

多态性是指类型的实例可以像其任何超类型的任何实例一样对待。多态性意味着“多种形式”。

假设您有一个名为 Dog 的类型。然后您就有一个名为 Spaniel 的类型,它继承自 Dog。 Spaniel 的实例可以在任何使用 Dog 的地方使用 - 它可以像任何其他 Dog 实例一样对待。这就是多态性。

方法重写是子类可以对基类中的方法执行的操作。 Dog 可能包含 Bark 方法。 Spaniel 可以重写该方法以提供更具体的实现。重写方法不会影响多态性 - 事实上,您在 Spaniel 中重写了 Dog 方法并不能使您能够或阻止您像对待狗一样对待 Spaniel。

方法重载只是给不同的方法赋予不同的参数相同的名称。

我希望这有帮助。

Polymorphism refers to the fact that an instance of a type can be treated just like any instance of any of its supertypes. Polymorphism means 'many forms'.

Say you had a type named Dog. You then have a type named Spaniel which inherits from Dog. An instance of Spaniel can be used wherever a Dog is used - it can be treated just like any other Dog instance. This is polymorphism.

Method overriding is what a subclass may do to methods in a base class. Dog may contain a Bark method. Spaniel can override that method to provide a more specific implementation. Overriding methods does not affect polymorphism - the fact that you've overriden a Dog method in Spaniel does not enable you to or prevent you from treating a Spaniel like a dog.

Method overloading is simply the act of giving different methods which take different parameters the same name.

I hope that helps.

韶华倾负 2024-09-18 23:48:50

坦率地说:

多态性是使用许多类型,这些类型在一个实现中具有特定的共同点,只需要共同的东西,而方法重载则使用一个 > 针对每种类型的实现。

Frankly:

Polymorphism is using many types which have specific things in common in one implementation which only needs the common things, where as method overloading is using one implementation for each type.

舞袖。长 2024-09-18 23:48:50

当你重写一个方法时,你就改变了它的实现。多态性将使用您的实现或基本实现,具体取决于您的语言(是否支持虚拟方法?)以及您创建的类实例。

重载方法是另一回事,它意味着使用具有不同数量参数的相同方法。

这种(重写)的组合,加上使用基类或接口并仍然在链上某处调用重写方法的可能性,称为多态性。

例子:

interface IVehicle
{
    void Drive();
}

class Car : IVehicle
{
    public Drive() { /* drive a car */ }
}

class MotorBike : IVehicle
{
    public Drive() { /* drive a motorbike */ }
}

class Program
{
    public int Main()
    {
        var myCar = new Car();
        var myMotorBike = new MotorBike();
        this.DriveAVehicle(myCar);        // drive myCar
        this.DriveAVehicle(myMotorBike);  // drive a motobike
        this.DriveAVhehicle();            // drive a default car
    }

    // drive any vehicle that implements IVehicle
    // this is polymorphism in action
    public DriveAVehicle(IVehicle vehicle)
    {
       vehicle.Drive();
    }

    // overload, creates a default car and drives it
    // another part of OO, not directly related to polymorphism
    public DriveAVehicle()
    {
        // typically, overloads just perform shortcuts to the method
        // with the real implemenation, making it easier for users of the class
        this.DriveAVehicle(new Car());
    }
}

When you override a method, you change its implementation. Polymorphism will use your implementation, or a base implementation, depending on your language (does it support virtual methods?) and depending on the class instance you've created.

Overloading a method is something else, it means using the same method with a different amount of parameters.

The combination of this (overriding), plus the possibility to use base classes or interfaces and still call an overriden method somewhere up the chain, is called polymorphism.

Example:

interface IVehicle
{
    void Drive();
}

class Car : IVehicle
{
    public Drive() { /* drive a car */ }
}

class MotorBike : IVehicle
{
    public Drive() { /* drive a motorbike */ }
}

class Program
{
    public int Main()
    {
        var myCar = new Car();
        var myMotorBike = new MotorBike();
        this.DriveAVehicle(myCar);        // drive myCar
        this.DriveAVehicle(myMotorBike);  // drive a motobike
        this.DriveAVhehicle();            // drive a default car
    }

    // drive any vehicle that implements IVehicle
    // this is polymorphism in action
    public DriveAVehicle(IVehicle vehicle)
    {
       vehicle.Drive();
    }

    // overload, creates a default car and drives it
    // another part of OO, not directly related to polymorphism
    public DriveAVehicle()
    {
        // typically, overloads just perform shortcuts to the method
        // with the real implemenation, making it easier for users of the class
        this.DriveAVehicle(new Car());
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文