简体   繁体   English

Java继承 - 可以将超类对象传递给子类进行简化吗?

[英]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 . 是的,你不能那样做因为VehicleCar的父母。 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.

相关问题 Java继承; 将子类传递给超类的抽象方法 - Java inheritance; passing a subclass to an abstract method of a superclass Java继承子类和超类 - Java Inheritance subclass and superclass 将超类对象作为参数传递给子类构造函数(java) - Passing superclass object as parameter to subclass constructor (java) 在 Java 中使用 Inheritance 将子类 Object 添加到超类数组 - Adding An Subclass Object To An Made of Superclass Array With Inheritance In Java Java 继承:在超类中调用子类方法 - Java Inheritance: Calling a subclass method in a superclass Java Inheritance:子类中的方法覆盖超类中的方法 - Java Inheritance: method in subclass overrides method in superclass 将子类或超类对象传递给方法(期望超类对象) - Passing subclass or superclass object to method (expecting a superclass object) 将超类 object 作为参数传递给具有子类参数的方法 - Passing a superclass object as a parameter to a method with a subclass parameter Java将子类实例数据传递给超类构造函数 - Java passing subclass instance data to superclass constructors Java中的继承-创建子类的对象还会调用超类的构造函数。 到底为什么 - Inheritance in Java - creating an object of the subclass invokes also the constructor of the superclass. Why exactly?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM