简体   繁体   English

扩展变量和协变返回类型

[英]Extending variables and covariant return types

I was testing out covariant return types and came across this problem. 我正在测试协变返回类型,并遇到了这个问题。

class Vehicle {

    int i = 3;
}
class Car extends Vehicle{

    int i = 5;

    public Car returningCar(){
        System.out.println("Returning Car");
        return new Car();
    }

    public Vehicle returningCarInVehicle(){
        System.out.println("Returning CarInVehicle");
        return new Car();
    }
}

public class ScjpTest{

    public static void main(String[] args){

        Car car = new Car();
        Vehicle vehicleCar = car.returningCar();
        Vehicle vehicleCar2 = car.returningCarInVehicle();

        System.out.println("vehicleCar " + vehicleCar.i);
        System.out.println("vehicleCar2 " + vehicleCar2.i);

    }
}

The output to the above is Returning Car 上面的输出是Returning Car

   Returning 
   CarInVehicle
   vehicleCar 3
   vehicleCar2 3

I dont understand why the output is 3. I was expecting the output to be 5 in both instances because at runtime the JVM uses the actual object not the reference. 我不明白为什么输出为3。我期望两个实例中的输出均为5,因为在运行时JVM使用的是实际对象而不是引用。

Thanks 谢谢

Fields aren't virtual/overrideable/etc. 字段不是虚拟/可覆盖/等。 They will be resolved according to the compile-time type of the reference, which in this case is Vehicle . 它们将根据引用的编译时类型(在这种情况下为Vehicle进行解析。

This code would print "vehicleCar2 5": 此代码将显示“ vehicleCar2 5”:

System.out.println("vehicleCar2 " + ((Car)vehicleCar2).i);

since the cast makes the expression of compile-time type Car . 因为强制转换使表达式成为编译时类型Car

You need to use methods to get the polymorphic behaviour you're after (it's also a best practice to encapsulate member variables by making them private and providing public setter and getter methods) 您需要使用方法来获得想要的多态行为(通过成员变量设置为私有并提供公共的setter和getter方法来封装成员变量也是一种最佳做法)

    class Vehicle {

        private int i = 3;

        protected Vehicle(int i) {
            this.i = i;
        }

        public int i() {
            return i;
        }
    }
    class Car extends Vehicle{

        public Car() { 
            super (5);
        }

        public Car returningCar(){
            System.out.println("Returning Car");
            return new Car();
        }

        public Vehicle returningCarInVehicle(){
            System.out.println("Returning CarInVehicle");
            return new Car();
        }
    }

    public static void main(String[] args){

        Car car = new Car();
        Vehicle vehicleCar = car.returningCar();
        Vehicle vehicleCar2 = car.returningCarInVehicle();

        System.out.println("vehicleCar " + vehicleCar.i());
        System.out.println("vehicleCar2 " + vehicleCar2.i());

    }

Your question is correct but Polymorphism works for functions only. 您的问题是正确的,但多态仅适用于函数。 It will not work for variable. 它不适用于变量。 It will take the reference type while executing variable not the exact object type that reference is pointing to.hope you will get it. 执行变量时将采用引用类型,而不是引用所指向的确切对象类型。希望您会得到它。

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

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