[英]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
尚未初始化,这会在您尝试比较s
和maxSpeed
时导致错误。
您正在尝试访问方法体外部的变量,它需要位于子类的构造函数或方法中,以便您以所需的方式访问它。
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.