[英]How to instantiate a super class static variable in sub class as final
I am simulating a parking lot application. 我正在模拟一个停车场应用程序。 And have a class
Vehicle
which is abstract and extended by concrete classes like Car
, Truck
etc. I have a variable parkingFare
which for each sub class of Vehicle
will be instantiated as class variable and final so that all instances of that particular class can use the same value. 并有一个
Vehicle
类,该类是由Car
, Truck
等具体类抽象和扩展的。我有一个变量parkingFare
,对于Vehicle
每个子类,它将实例化为类变量和final,以便该特定类的所有实例都可以使用相同的值。 eg If I set parkingFare
for Car
as $2 it should be the same for all instances of Car
. 例如,如果我设置
parkingFare
的Car
,用$ 2,应该是所有场合的同一Car
。 The problem is I am unable to access this variable in my concrete class as class variable. 问题是我无法在具体类中以类变量的形式访问此变量。
public abstract class Vehicle {
String plateNo;
LocalTime startTime;
static int parkingFare;
public abstract int getFare();
public abstract String getPlateNo();
public abstract LocalTime getParkedTime();
}
public class Car extends Vehicle {
final parkingFare = 2; // how I want to instantiate it.
public Car(String plateNo){
this.plateNo = plateNo;
this.startTime = LocalTime.now();
}
@Override
public LocalTime getParkedTime(){
return startTime;
}
@Override
public String getPlateNo(){
return plateNo;
}
@Overrid
public int getFare(){
return parkingFare;
}
}
Use a static-block: 使用静态块:
public class Car extends Vehicle {
static {
parkingFare = 2;
}
// ...
}
However, this makes parkingFare equal in all Vehicle-classes, regardless of subclass. 但是,这使得在所有车辆类中,无论子类如何,parkingFare均相等。 For example, having another Vehicle subclass Bus setting parkingFare = 10 and running:
例如,让另一个Vehicle子类的Bus设置parkingFare = 10并运行:
Car car = new Car("Car");
Bus bus = new Bus("Bus");
System.out.println(car.getFare());
System.out.println(bus.getFare());
Results in 10 being printed twice. 结果10被打印两次。
Adding parkingFare to each subclass will solve this. 向每个子类添加parkingFare将解决此问题。
(Double edit: confused with how java stores static variables, but then I tested, and the current description is accurate) (双重编辑:与java如何存储静态变量相混淆,但随后我进行了测试,当前描述是准确的)
Use this in Your SubClass 在您的子类中使用它
static {
parkingFare=10;
}
Since A static variable is related to a class not in the instance, so all instances of Class will share the static variable.
由于静态变量与实例中未涉及的类相关,因此Class的所有实例都将共享静态变量。
So after initializing parkingFare=10
all the instances of Class Car
will have the same parking fare . 因此,在初始化
parkingFare=10
所有Class Car
实例将具有相同的停车费。
Now Coming to your Comment you need to make them final in child class you can't ,if you want to make them final
you need to declare parkingFare
as final itself in Vehicle Class
现在来发表您的评论,您需要将它们设为不能在儿童班级中决赛,如果要使其
final
,则需要在Vehicle Class
声明parkingFare
为最终决赛。
I think if you want to make cars cost 2$, then you should move the static final variable to that class. 我认为,如果您想使汽车的价格为2美元,则应将static final变量移至该类。
public abstract class Vehicle {
String plateNo;
LocalTime startTime;
public abstract int getFare();
public abstract String getPlateNo();
public abstract LocalTime getParkedTime();
}
public class Car extends Vehicle {
static final int parkingFare = 2;
...
@Override
public final int getFare() {
return parkingFare;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.