简体   繁体   English

继承与多态之间的区别

[英]Difference between inheritance & polymorphism

I am having issues understanding what exactly polymorhism is and how it differs from inheritance. 我在理解什么是多态性以及它与继承有何不同方面存在问题。 I have researched the web and found many answers that gives very technical deffinitions on what polymorphism is but nothing in which i really understand too well. 我研究了网络,并找到了许多答案,这些答案对多态性是什么提供了非常技术上的定义,但我对它的理解并不多。 I have come up with an example that might be polymorphism but may not be. 我想出了一个例子,可能是多态性,但可能不是。

MY example: 我的例子:

Say you have a Gym with many levels of membership. 假设您拥有一个拥有许多会员级别的体育馆。

  1. basic member 基本成员
  2. silver member 银卡会员
  3. gold member 黄金会员

and each class is a basic member but has more function 每个班级都是基本成员,但功能更多

class basicMember(){
private double rate = 10;
privatedbl poolPrice = 5;

public void setRate (dbl in);
{
 rate = in;
}
public dbl getRate()
{
 return rate;
}
public dbl getPoolPrice()
{
 return poolPrice;
}
} 

class silverMember extends basicRate()
{
 private dbl poolPriceDis = .9;
setRate(15);

public dbl getPoolPriceDis(){
return getPoolPrice() * poolPriceDis;
}
 } 

class goldMember extends basicRate(){
private dbl poolPriceDis = .85;
setRate(20);

public dbl getPoolPriceDis(){
return getPoolPrice() * poolPriceDis;
}
}

Would this be an example of inheritance or polymorphism or both? 这是继承或多态性还是两者的一个例子? Please explain..... 请解释.....

Your example demonstrates inheritance. 您的示例演示了继承。

  • Polymorphism lets you call methods of a class without knowing the exact type of the class. 多态使您可以在不知道类的确切类型的情况下调用类的方法。 It deals with how the program decides which methods it should use, depending on what type of object it has. 它处理程序如何决定其应使用的方法,具体取决于程序具有的对象类型。 If you have a Animal, which has a eat method, and you have a Cow which extends Animal, which has its own implementation of Eat, which method gets called is depends if you have a pointer to Animal or a Cow. 如果您有一个动物,它具有一个eat方法,并且您有一个Cow扩展了Animal,它具有自己的Eat实现,则调用哪种方法取决于您是否有指向Animal或Cow的指针。

Polymorphism allows you to write a method that works for any Animal: 多态允许您编写适用于任何Animal的方法:

public void pet(Animal animal) {
   ...
}

This method would accept Dog, Cat, etc, including subclasses of Animal that are yet to be written. 此方法将接受Dog,Cat等,包括尚未编写的Animal子类。

  • Inheritance lets derived classes share interfaces and code of their base classes. 继承使派生类共享其基类的接口和代码。 It is when a 'class' derives from an existing 'class'. 这是“类”从现有“类”派生的时间。 So if you have a Animal class, then you have a Cow class that extends Animal, Cow inherits all the things that Animal has (depending on access modifiers). 因此,如果您有Animal类,那么您有一个Cow类扩展了Animal,Cow继承了Animal具有的所有内容(取决于访问修饰符)。

    public class Animal {...} 公共类动物{...}

    public class Dog extends Animal {...} 公共课狗延伸动物{...}

    Dog d = new Dog(); Dog d = new Dog();

    d.DoAnimalStuff(); d.DoAnimalStuff();

Inheritance in any language is a type of polymorphism. 任何语言的继承都是一种多态性。 Polymorphism is the ability for multiple things (multiple classes, multiple functions, etc) to be treated in the same way. 多态是指以相同的方式处理多个事物(多个类,多个函数等)的能力。 Inheretance allows that because you can treat any type as the base class. Inheretance允许这样做,因为您可以将任何类型视为基类。 Another form of polymorphism in Java is generics- you can pass in any type, so long as it defines all the functions you need. Java中多态的另一种形式是泛型-您可以传递任何类型,只要它定义了您需要的所有功能即可。

Inheritance is one object having the same logic as another object with some extra logic. 继承是一个逻辑与另一个具有相同逻辑的对象。 Inheritance saves you lines of code. 继承可以节省代码行。

Polymorphism is allowing multiple objects to be type-safely stored in the same place, and operated on with the same code 多态性允许将多个对象安全地存储在同一位置,并使用相同的代码进行操作

suppose d inherits/impliments from b 假设db继承/暗示

Base b = new Derived d;

b.DoThingsThatBasesCanDo();

Your example is just inheritance, but it can become polymorphism depending on your usage 您的示例只是继承,但是根据您的用法,它可能会变成多态

It would be even more polymorphic if poolPriceDis was a member of your base class, and you either override it in your derived classes or initialized it in your respective class's constructers 如果poolPriceDis是基类的成员,则它会更加多态,您可以在派生类中重写它,或者在各自类的构造函数中对其进行初始化。

Using Java... 使用Java ...

Inheritance is the concept of defining a class that adds to, or extends, another class. 继承是定义添加或扩展另一个类的类的概念。 It is also possible to change a class. 也可以更改类别。 For example, a boss might extend an employee by adding the ability to ask for a raise. 例如,老板可以通过增加要求加薪的能力来扩展员工。

public class Employee
{
    ...
}

public class Boss extends Employee
{
    public void askForRaise(){}
}

Polymorphism, can be used a few ways. 多态,可以使用几种方法。 Lets look at two of them. 让我们看看其中两个。 First, different classes can do different things with the same method, for example, we can define a Shape class that represents all shapes and has a draw method. 首先,不同的类可以使用相同的方法执行不同的操作,例如,我们可以定义一个Shape类,该类表示所有形状并具有draw方法。

public abstract class Shape
{
    public abstract void draw();
}

A square will implement draw to draw a square, while a circle will draw a circle. 正方形将执行绘制以绘制正方形,而圆形将绘制圆形。 Both draw, but do different things, this is the first kind of polymorphism. 两者都绘制但做不同的事情,这是第一种多态。

public class Square extends Shape
{
    public void draw(){...}
}

public class Circle extends Shape
{
    public void draw(){...}
}

It is worth noting that we also used inheritance in this example. 值得注意的是,在此示例中我们还使用了继承。 Square and Circle inherit from Shape so they have all of the methods and properties that Shape has. Square和Circle从Shape继承,因此它们具有Shape具有的所有方法和属性。

A second kind of polymorphism that shows up in Java is the ability to have the same method with different arguments. Java中出现的第二种多态性是具有相同方法和不同参数的能力。 For example, a number might have an add method that takes an integer and another that takes a double: 例如,一个数字可能有一个采用整数的加法,而另一个则采用双精度法:

public class MyNumber
{
    public void add(int i){...}
    public void add(double d){...}
}

Having two versions of the same method is another form of polymorphism. 具有相同方法的两个版本是多态的另一种形式。

Polymorphism comes in many shapes and sizes, one of which is inheritance. 多态有多种形状和大小,其中之一就是继承。

Inheritance lets you use objects as if they were of the same class, where in reality they may be different classes. 继承使您可以像使用同一类一样使用对象,而实际上它们可能是不同的类。 See your example code. 查看示例代码。

Overloading lets you use functions with parameters of different types as if they were the same. 通过重载,您可以将具有不同类型参数的函数用作相同的函数。 For example: add("hello", "world") and add(1, 2). 例如:add(“ hello”,“ world”)和add(1,2)。

Parametrization lets you use functions or classes that don't care about the type, as long as you tell it in advance. 参数化使您可以使用与类型无关的函数或类,只要您事先告知即可。 For example: Vector<Float> versus Vector<String>. 例如:Vector <Float>与Vector <String>。

In all cases you see this "different/many" (poly) acting as the "same/similar shape" (morph). 在所有情况下,您都会看到此“不同/许多”(多边形)充当“相同/相似形状”(变形)。

Quoting Mr. Cay Horstmann from Object Oriented Design & Patterns : 引用面向对象设计与模式中的 Cay Horstmann先生:

The ability to select the appropriate method for a particular object is called polymorphism . 为特定对象选择适当方法的能力称为多态性 (The term "polymorphic" literally means "having multiple shapes".) (术语“多态”的字面意思是“具有多种形状”。)

and

You use inheritance to model a relationship between classes in which one class represents a more general concept and another a more specialized concept. 您可以使用继承对类之间的关系进行建模,其中一个类代表一个更通用的概念,另一个代表更专业的概念。

In your case, you're just using inheritance. 就您而言,您只是在使用继承。 But if you would override, let's say, your setRate() method in the silverMember class and had something like this in the main: 但是,如果您要覆盖SilverMember类中的setRate setRate()方法,并且在主类中具有以下内容:

basicMember member = new silverMember();
member.setRate(20);

then the setRate() of the silverMember class is invoked although you have a basicMember variable. 然后,尽管您具有basicMember变量,但将调用silverMember类的setRate() That's polymorphism. 那是多态性。

-- -

I wouldn't be true to myself if i did not advise you not to do this interview if you don't know this kind of stuff. 如果我不建议您如果您不了解这种事情,那么我对自己不忠实。 Try reading the book i quoted in the beginning of the comment. 尝试阅读我在评论开头引用的书。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM