Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/318.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 关于构造不带super()扩展抽象类构造函数的子类的问题_Java_Oop_Inheritance - Fatal编程技术网

Java 关于构造不带super()扩展抽象类构造函数的子类的问题

Java 关于构造不带super()扩展抽象类构造函数的子类的问题,java,oop,inheritance,Java,Oop,Inheritance,我是Java OOP新手,一直在学习这方面的知识,正在阅读一本名为《Java中的AP计算机科学》的GitBook 我发现这一页非常混乱: 代码是这样的 public abstract class VehicleClass { private String type; private String vehicleName; public VehicleClass(String vType, String vName) { type = vType;

我是Java OOP新手,一直在学习这方面的知识,正在阅读一本名为《Java中的AP计算机科学》的GitBook 我发现这一页非常混乱:

代码是这样的

public abstract class VehicleClass {
    private String type;
    private String vehicleName;

    public VehicleClass(String vType, String vName) {
        type = vType;
        vehicleName = vName;
    }

    /* This will need to be abstract, since 
     * we will need to implement different formulas
     * depending on if the vehicle is electric or 
     * gas powered.
     */ 
    public abstract double getMileage();
}

/* As you can see, in both classes, we have `getMileage` implemented
 * with different formulas.
 */ 
public class Truck extends VehicleClass {
    private double gasTankCapacity;
    private double milesPerTank;

    public Truck(double capacity, double miles) {
        gasTankCapacity = capacity;
        milesPerTank = miles;
    }

    public double getMileage() {
        return milesPerTank/gasTankCapacity;
    }
}

public class ElectricCar extends VehicleClass {
    private double maxCharge;
    private double milesPerCharge;
    private double maxEnergyUsage;

    public ElectricCar(double charge, double maxEnergy, double milesCharge) {
        maxCharge = charge;
        maxEnergyUsage = maxEnergy;
        milesPerCharge = milesCharge;
    }

    public double getMileage() {
        return (maxCharge*milesPerCharge)/maxEnergyUsage;
    }
}
我的问题是: 1.为什么他不用
super()
构建两个子类的构造函数。他不需要将type和
vehicleName
传递给超类的构造函数吗?
2.为什么他要将超级类中的type和
vehicleName
设置为
private
?我知道子类不能从超类继承实例变量。他为什么不改为使用protect呢?

你说得对,因此它无法编译

如果super类中有这样的构造函数,则只能省略对super的无参数调用,在这种情况下,它会自动推断出来

推测原因:作者想展示其他东西(如评论中所述),可能首先有一个可行的解决方案,但后来决定,vtype代码将示例膨胀得太多,并将其删除,而无需再次测试

对于问题2,为什么他将超类中的type和vehicleName设置为private

我会说,你继承了实例变量,不管是私有的还是非私有的,你只是不能直接访问它们

他为什么不使用protect呢

我们不知道。因为它只是演示代码,并且私有成员没有被使用,也不能通过getter或setter访问,所以我们无法判断。也许这是一个后来被扩展然后被使用的例子

  • 你完全正确!那段代码无法编译

  • 如果不知道这些变量和类的用途,很难说


  • 2) 这很可能是一个项目的部分代码,或者您稍后看到的更完整的示例。一般来说,您在没有理由不使用的任何事情上都使用private,因为您不希望外部类改变您的内部状态。@Gabeschen谢谢,但我仍然不完全理解。如果我们对类型vehicleName使用private,那么子类就不能继承它们,那么这是否意味着子类没有类型和vehicleName?它们有。子类无法直接访问它。但它可以调用父类中定义的任何函数,这些函数可以操作或使用对它可见的值(使用子类的外部代码也可以)。