简体   繁体   English

如何将子类中的超类静态变量实例化为final

[英]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类,该类是由CarTruck等具体类抽象和扩展的。我有一个变量parkingFare ,对于Vehicle每个子类,它将实例化为类变量和final,以便该特定类的所有实例都可以使用相同的值。 eg If I set parkingFare for Car as $2 it should be the same for all instances of Car . 例如,如果我设置parkingFareCar ,用$ 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.

相关问题 在java中的子类(子类)中初始化静态最终变量 - initialize static final variable in child class(sub class) in java 子类变量未隐藏超类中的变量 - Sub class variable is not hiding the variable in super class 如何从通用超类访问子类变量 - How To Access Sub Class Variable From Generic Super Class 如何在静态类中初始化最终的静态变量? - How can I initialize final static variable in a static class? 如何模拟在另一个类中使用另一个静态最终变量的变量 - How to mock a variable that uses another static final variable in another class 如何编写面向 Object 的程序来实例化具有超级 class、子类和接口的对象和接口 - How to write an Object Oriented Program to instantiate objects and interfaces with super class, sub-classes and interfaces 如何从java中的其他类初始化静态最终变量 - how to initialize static final variable from other class in java 我如何从另一个包中实例化Java内部的public static final类 - How can I instantiate a inner public static final class in java from a different package 公共静态最终类变量中的不同句柄 - different handles in public static final class variable JUnit Mockito 使用静态类最终变量 - JUnit Mockito using a static class final variable
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM