简体   繁体   English

用Java创建对象(继承和多态)

[英]Creating objects in Java (Inheritance & Polymorphism)

What is the difference between these two object creations where car is the super class and toyota is the subclass... 这两个对象创作之间有什么区别,其中汽车是超级类,而丰田是子类......

car t = new toyota(); car t = new toyota(); toyota t = new toyota(); toyota t = new toyota();

(I believe we cant do something like this : toyota t = new car(); .... Why ?) (我相信我们不能做这样的事情: 丰田t =新车(); ....为什么?)

Inheritance is confusing me and so is polymorphism... Any help would be appreciated 继承让我感到困惑,多态性也是如此......任何帮助都会受到赞赏

The difference is in the type of the object t : in the first case, only car 's methods are available, while in the second case you also get the toyota -specific methods, if any. 不同之处在于对象的类型t :在第一种情况下,只有car的方法可用,而在第二种情况下,你也可以得到toyota特有的方法,如果有的话。

Here is an example: 这是一个例子:

public class car {
    public void drive() {...}
    public void stop() {...}
}
public class toyota extends car {
    public void drive() {... /*toyota-specific code*/}
    public void stop() {... /*toyota-specific code*/}
    public void rollUpWindows() {...}
}

If you declare 如果你宣布

car c = new toyota();

you can call drive and stop , but not rollUpWindows . 你可以调用drivestop ,但不能rollUpWindows If you declare 如果你宣布

toyota c = new toyota();

you can call all three methods. 你可以调用这三种方法。

There is a general concept of programming to an interfaces which is similar to case #1 above. 存在对接口进行编程的一般概念,其类似于上面的情况#1。

The creation is the same. 创作是一样的。 How you access the created object is different. 如何访问创建的对象是不同的。 Let's use a slightly modified example: 让我们使用一个略微修改的例子:

Car c = new ();
AutomaticTransmissionCar a = new AutomaticTransmissionCar();
StandardTransmissionCar s = new StandardTransmissionCar();
Car c = new AutomaticTransmissionCar();

a.drive();
s.drive();
c.drive();

Presumably, the drive() method for an AutomaticTransmissionCar is much different than the drive() method of a StandardTransmissionCar . 据推测, drive()用于方法AutomaticTransmissionCar比太大的不同drive()一个的方法StandardTransmissionCar This is the key of polymorphism: when we call c.drive() , the car automatically determines the correct drive() method to use. 这是多态的关键:当我们调用c.drive() ,汽车会自动确定要使用的正确drive()方法。

(Note that this is probably not the best design, and is only used to illustrate the concepts here.) (请注意,这可能不是最佳设计,仅用于说明此处的概念。)

Let's start with the second creation (it's the easiest one) : Here you're creating a Toyota object and for the entire course of your program this will be a Toyota with all his specific properties and methods AND the protected/public properties and protected/public methods it INHERITED from car. 让我们从第二次创作开始(这是最简单的一次):在这里你要创建一个丰田对象,在你的程序的整个过程中,这将是一个拥有所有特定属性和方法以及受保护/公共属性和受保护/的丰田公共方法它来自汽车。

The first creation is also valid but is polymorfic, drawback is that you won't be able to address the Toyota specific properties and method, because it has only been declared as a Car. 第一个创建也是有效的但是是多边形的,缺点是你将无法解决丰田特定的属性和方法,因为它只被声明为Car。 However deep down it's a Toyota, so when you do this 不过在内心深处它是一辆丰田汽车,所以当你这样做的时候

Toyota t2 = (Toyota)t; 

You've changed (casted) it to a Toyota. 你把它改成(铸造)给了丰田。

The first creation works because a Toyota is also a Car. 第一次创作是因为丰田也是一辆汽车。 The other way around doesn't work because a Car isn't always a Toyota, it can be a BMW of a Lexus, .... and because the compiler has no certain way a telling what is can be, this is not allowed. 反过来不起作用,因为汽车并不总是丰田,它可以是雷克萨斯的宝马,......并且因为编译器无法确定可能是什么,这是不允许的。

Little tip : inheritance is easy if you draw the inheritance tree. 小提示:如果绘制继承树,继承很容易。 Put superclass on top and subclasses under it and so on. 将超类放在顶层,将子类置于其下,依此类推。 Inhertance works traveling down, not traveling up 旅行工作向下行驶,而不是向上行驶

Hope this clears it a bit 希望这有点清除它

In your example, toyota extends car . 在你的例子中, toyota扩展了car This means that toyota is a (or rather, isA ) kind of a car . 这意味着toyota是一种(或更确切地说, isA一种) car Every toyota is also a car , but not every car is a toyota (eg, some of them may be Hondas). 每个toyota也是一辆car ,但不是每辆car都是toyota car (例如,其中一些可能是本田汽车)。

If t is a reference to a car , it can hold any type of car - a Toyota, a Honda, a Ferrari, you name it. 如果t是对car的引用,它可以容纳任何类型的car - 丰田,本田,法拉利,你的名字。 However, if t is defined as a toyota , it can't hold any old car , just toyota s. 然而,如果t被定义为toyota ,它不能容纳任何car ,只有toyota

Think of it this way: car is a superclass and toyota is subclass (meaning it inherits from car), in other words it makes sense to say that each toyota is a car, but not that each car is toyota. 可以这样想:汽车是一个超类,丰田是子类(意思是它继承自汽车),换句话说,说每辆丰田都是汽车是有意义的,但不是每辆汽车都是丰田汽车。 This is an example of IS-A relationship between two classes (Toyota IS-A car). 这是两个类(丰田IS-A汽车)之间的IS-A关系的一个例子。

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

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