简体   繁体   English

子类在Java中从Parent类更改受保护的变量

[英]Child class Changing a protected variable from a Parent class in Java

I know that it is bad design to have a protected variable in a parent class because all child classes can change that value. 我知道在父类中拥有受保护的变量是不好的设计,因为所有子类都可以更改该值。 However, I was trying to test it out but I am doing something wrong here. 但是,我试图测试它,但我在这里做错了。 It's telling me that cannot find symbol speed = 999999; 它告诉我找不到符号speed = 999999; in Truck class. 在卡车类。 I thought the child class has access to the protected variable speed in parent class. 我认为子类可以访问父类中受保护的变量speed

public class Vehicle {
    protected double speed;
    protected double maxSpeed;

    public Vehicle(double speed, double maxSpeedIn) throws InvalidDataException{
        setSpeed(speed);
        maxSpeed = maxSpeedIn;
    }

    public void setSpeed(double s) throws InvalidDataException {
        if (s < 0.0) {
            throw new InvalidDataException("Negative speed is not valid" );
        }
        if (s > maxSpeed) {
            throw new InvalidDataException("Speed cannot exceed maximum spped:");
        }
        speed = s;
    }


}

public class Truck extends Vehicle {

    public Truck(double speedin, double maxSpeedin) throws InvalidDataException {
        super(speedin,maxSpeedin);
    }

    speed = 999999;

}

Your speed = 99999; 你的速度= 99999; line isn't valid the way you placed it in the Truck class. line放在Truck类中的方式无效。 Try to put it somewhere else. 试着把它放在其他地方。

You could for example, just for your testing purpose, put it inside Truck 's constructor, after the call to super . 例如,您可以在调用super之后将其放入Truck的构造函数中,仅用于测试目的。

Note that you'd have had the exact same error if you had chosen another name altogher, like this: 请注意,如果您选择了另一个名称altogher,则会遇到完全相同的错误,如下所示:

public Truck extends Vehicle {

    public Truck(double speedin, double maxSpeedin) throws InvalidDataException {
        super(speedin,maxSpeedin);
    }

    justTesting = 999999;

}

In Java you cannot simply write instructions (like speed = 999999; ) in the middle of a class. 在Java中,您不能简单地在类的中间编写指令(如speed = 999999; )。 Instructions must be written inside a method (function). 必须在方法(函数)中写入指令。 What actually did you mean? 你究竟是什么意思? When do you want this instruction to be carried out? 你什么时候想要执行这个指令?

By the way, when setSpeed is called by the Vehicle constructor maxSpeed is not yet initialised, which will cause an error when you try to compare s and maxSpeed . 顺便说一下,当Vehicle构造函数maxSpeed时, setSpeed尚未初始化,这会在您尝试比较smaxSpeed时导致错误。

您正在尝试访问方法体外部的变量,它需要位于子类的构造函数或方法中,以便您以所需的方式访问它。

In my opinion protected variables doesn't have to be a bad thing. 在我看来,受保护的变量不一定是坏事。 In some cases it really can be a necessity. 在某些情况下,它确实是必要的。 But it depends on the design of course :) 但这取决于当然的设计:)

In this case I think it makes perfect sense. 在这种情况下,我认为这是完全合理的。 And you should be able to change the speed value in the Truck class. 您应该能够更改Truck类中的speed值。 The problem here is (probably) that the line speed = 999999; 这里的问题是(可能)线speed = 999999; isn't placed inside a method in the class. 不是放在类中的方法内。 I might be wrong, but it would seem to me Java compiles it as code unrelated to the class, and thus the speed variable cannot be found. 我可能错了,但在我看来,Java会将其编译为与类无关的代码,因此无法找到速度变量。 Try putting it in the constructor and see what happens. 尝试将它放在构造函数中,看看会发生什么。

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

相关问题 如何在Java中抽象父级的子类中初始化受保护的最终变量? - How to initialize a protected final variable in a child class of an abstract parent in Java? 受保护的变量在 Java 中的子 class 内不可访问 - Protected variable not accessible within child class in Java 无法访问 Java 子类中父 Kotlin 类的受保护成员变量 - Not able to access protected member variable of Parent Kotlin class in Java child class 在Java中将父类分配给子类变量 - Assign parent class to child class variable in Java 使用子类和抽象父类中受保护的成员变量 - The use of protected member variables from child class and abstract parent 防止在不使用Java中的getter和setter的情况下从子类访问受保护的变量 - prevent protected variable access from child class without using getter and setter in java 当变量是抽象父类时,Java将分派给子类 - Java dispatch to child class when variable is of abstract parent class Java:使用父类方法访问子类变量 - Java : Using parent class method to access child class variable 为什么子类对象在Java中调用父类字段变量? - Why is the child class object invoking the parent class field variable in Java? 在 Java 中,是否可以使用超级关键字从子 ZA2F2ED4F8EBC2CBB4C21A2 引用父 class 的 static 变量? - In Java , is it possible to refer static variable of parent class from child class using super keyword?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM