[英]Java inheritance - could passing superclass object into a subclass be simplified?
We know that 我们知道
String s = new Object();
will result in error: 会导致错误:
incompatible types: Object cannot be converted to String
Thus, from the following example, we cannot do: 因此,从以下示例中,我们不能这样做:
Car car = new Vehicle();
But what would be wrong for Java having something like this: 但是Java有这样的东西会有什么问题:
Supperclass: Supperclass:
public class Vehicle {
String color;
int numberOfWheels;
int maxSpeed;
// etc.
}
Subclass: 子类:
public class Car extends Vehicle {
String engineType;
String transmissionType;
int numberOfDoors;
// etc.
Car(Vehicle vehicle) {
// the theoretical idea of passing
// the superclass object within a subclass
super = vehicle;
}
}
The 'super = vehicle;' '超级=车辆;' would allow us to pass all values of a previously set superclass (vehicle) to new subclasses (car) at one shot.
允许我们一次性将先前设置的超类(车辆)的所有值传递给新的子类(汽车)。 And the usage would be:
用法是:
public class App {
public static void main(String[] args) {
Vehicle vehicle = new Vehicle();
vehicle.color = "GREEN";
vehicle.maxSpeed = 100;
vehicle.numberOfWheels = 4;
Car car = new Car(vehicle);
// this would print: GREEN
System.out.println("Car color is: " + car.color);
}
}
Perhaps there already is a simple way of doing it similarly. 也许已经有一种类似的简单方法。
"Enlighten those who are still in dark ... " “启迪那些仍在黑暗中的人......”
You can do something similar to that, but you still need the provide the Car
-specific information (either as arguments to the Car
constructor, or defaults, or a combination of both). 您可以执行与此类似的操作,但仍需要提供
Car
specific特定信息(作为Car
构造函数的参数,或默认值,或两者的组合)。
One fairly common way is to define a copy-constructor for Vehicle
in Vehicle
: 一种相当常见的方法是为
Vehicle
in Vehicle
定义一个copy-constructor :
public Vehicle(Vehicle other) {
this.color = other.color;
this.numberOfWheels = other.numberOfWheels;
this.maxSpeed = other.maxSpeed;
}
Then in Car
, you use that copy-constructor and then flesh out the Car
details: 然后在
Car
,使用该复制构造函数,然后充实Car
详细信息:
public Car(Vehicle v/*, and possibly car-specified args...*/) {
super(v);
// ...fill in `Car`-specific information
}
Thus, from the following example, we cannot do:
因此,从以下示例中,我们不能这样做:
Car car = new Vehicle();
Yeah, you cannot do that because Vehicle
is the parent of Car
. 是的,你不能那样做因为
Vehicle
是Car
的父母。 So, you can do: 所以,你可以这样做:
Vehicle car = new Car();
That is part of polymorphism. 这是多态性的一部分。 And the simple way to do what you want to do is,
做你想做的简单方法是,
First add constructor to Vehicle
class, 首先将构造函数添加到
Vehicle
类,
public Vehicle(String color, int numberOfWheels, int maxSpeed) {
this.color = color;
//other two assignment
}
Then in Car
class constructor, 然后在
Car
类构造函数中,
public Car(String color, int numberOfWheels, int maxSpeed, String engineType, String transmissionType, int numberOfDoors) {
super(color, numberOfWheels, maxSpeed);
this.engineType = engineType;
this.transmissionType = transmissionType;
this.numberOfDoors = numberOfDoors;
}//keep remember, you can also use constructor overloading.
Now inside main()
, 现在在
main()
里面,
Vehicle car = new Car(/*arguments*/);
You can do similar as 你可以这样做
public Car(Vehicle v){
super(v)
// assuming super has constructor to copy from Vehicle
}
Doing that would not be good as any methods called on car would be the methods found in the baseclass, or atleast that is what happens in the normal case when it is the otherway around. 这样做不会很好,因为在汽车上调用的任何方法都是在基类中找到的方法,或者至少是在正常情况下发生的情况。 It's the object's methods that gets called, not the variable's.
这是被调用的对象的方法,而不是变量的方法。
Since you declare Vehicle as non-abstract class, JVM look at it as instantiable class. 由于您将Vehicle声明为非抽象类,因此JVM将其视为可实例化的类。
super and this are references with particular meaning in Object oriented languages. super,这是在面向对象语言中具有特殊含义的引用。 If you were familiar with Object Oriented Compilers and runtime environments, you would be get why you cannot access derived classes.
如果您熟悉面向对象的编译器和运行时环境,那么您就会明白无法访问派生类的原因。
Imagine you you have to Concrete classes which inherit Vehicle, so which concrete class you want to refer? 想象一下,你必须使用继承Vehicle的具体类,那么你要引用哪个具体类?
Another issue, Compiler take space in heap according to the pattern, it Collect all information (Attributes and behaviors) as a single building block for objects, override Attributes and reserve space for all methods, this reference means all methods which are solidly defined in concert class and non-overriden methods of super class, (Super class is only concept in development, at run time it's space will merge with it's concert class), super reference means all methods which are overriden by it's subclasses but still the method's code space is addressed by the building block. 另一个问题,编译器根据模式占用堆中的空间,它将所有信息(属性和行为)收集为对象的单个构建块,覆盖属性并为所有方法保留空间,此引用表示在演唱会中稳定定义的所有方法超类的类和非重写方法,(超类只是开发中的概念,在运行时它的空间将与它的Concec类合并),超级引用意味着所有被它的子类覆盖的方法,但方法的代码空间仍然是由构建块解决。
by these simple issues you will find out that changing super reference or this reference is impossible and will round the OOP Concepts. 通过这些简单的问题,您会发现更改超级引用或此引用是不可能的,并将围绕OOP概念。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.