Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Oop 为什么维基百科会说;多态性与方法重载或方法重写不同;_Oop_Polymorphism_Overloading_Overriding - Fatal编程技术网

Oop 为什么维基百科会说;多态性与方法重载或方法重写不同;

Oop 为什么维基百科会说;多态性与方法重载或方法重写不同;,oop,polymorphism,overloading,overriding,Oop,Polymorphism,Overloading,Overriding,我环顾四周,没有发现任何类似的问题 以下是我从中得到的段落: 多态性与方法重载或方法重写不同。多态性只涉及将特定实现应用于接口或更通用的基类。方法重载是指在同一类中具有相同名称但不同签名的方法。方法重写是指子类替换其父类的一个或多个方法的实现。方法重载和方法重写本身都不是多态性的实现 这里有人能更清楚地解释一下吗,尤其是“多态性与方法重写不同”部分?我现在很困惑。提前感谢。重写方法时,会更改其实现。多态性将使用您的实现或基本实现,具体取决于您的语言(它支持虚拟方法吗?)和您创建的类实例 重载一个

我环顾四周,没有发现任何类似的问题

以下是我从中得到的段落:

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


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

重写方法时,会更改其实现。多态性将使用您的实现或基本实现,具体取决于您的语言(它支持虚拟方法吗?)和您创建的类实例

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

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

例如:

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());
    }
}

多态性与被重写的方法无关;它是关于确定特定流程实现的对象。继承是一个简单的例子,但决不是唯一的例子:

小说是一种书。它有很多相同的方法,你可以对一本书做的一切也可以对一本小说做。因此,任何接受书作为论据的方法也可以将小说作为论据处理。(示例包括.read()、.write()、.burn()。就其本身而言,这并不是指小说可以改写书本的事实。相反,它指的是抽象的特性。如果一位教授指定一本书去读,他/她不在乎你怎么读,只在乎你怎么读。类似地,调用程序并不关心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)
多态性(非常简单地说)是一种在需要基类的情况下使用派生类的可能性:

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 */ }
}

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

在同一个类中,重载有许多同名但参数不同的方法

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

多态性是这样一个事实:当作为参数给定时,继承类的实例可以替换基类的实例

例如:

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.
  }
}
看到了吗?
多态性是指同一对象可以用作其自身类、基类的实例,甚至用作接口。

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


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

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

假设你有一种狗。然后有一个名为Spaniel的类型继承自Dog。在任何使用狗的地方都可以使用斯班尼犬,它可以像对待其他狗一样对待。这是多态性

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

方法重载就是给不同的方法赋予相同的名称,这些方法使用不同的参数

我希望这能有所帮助。

坦率地说:


多态性使用的是许多类型,这些类型在一个只需要普通东西的实现中有特定的共同点,其中as方法重载是对每个类型使用一个实现。

我不认为多态性=重载+重写,重载实际上与面向对象无关。你可以使用C实现重载。@Clement:我认为他的最后一句可能措词不当(我认为“this”的意思是用于覆盖,而不是重载)。@Clement/@Adam:你们都是对的:重载是OO的核心概念(也是非OO语言,请查看OOSC第95页),但不是多态性本身。然而,语言的影响
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.
  }
}