创建子 class 时,不会创建来自超级 class 的一个变量 (JAVA)

When creating a subclass one variable from the super class isn't created (JAVA)

我目前正在为一个奇怪的问题而苦苦挣扎.. 我正在从一个名为 StandardTruck 的子 class 创建一个实例,它继承自超级 class Truck。 来自 Truck class 和 StandardTruck subclass 的所有变量都被初始化和创建,除了一个 - truckID

变量 truckID 应该在每个 Truck 实例构造后增加整数,所以我认为它应该是静态变量

public abstract class Truck {

    static private int truckID = 2000;
    private String licensePlate;
    private String truckModel;
    private boolean available= true;
    private int timeLeft;
    private ArrayList<Package> packages = new ArrayList<Package>();
    
    
    //Constructors
    public Truck() {
        this.licensePlate = randomNumber(100,999) + "-" + randomNumber(10,99) + "-" + randomNumber(100,999);
        this.truckModel= "M"+ randomNumber(0,4);
        truckID++;
    }
    
    public Truck(String licensePlate, String truckModel) {
        this.licensePlate=licensePlate;
        this.truckModel=truckModel;
        truckID++;
    }
public class StandardTruck extends Truck{
    
    private int maxWeight;
    private Branch destination;
    
    
    //Constructors
    public StandardTruck() {
        super();
        maxWeight = randomNumber(200, 400);
        System.out.println("Creating "+ this.toString());
        
    }
    '''
    public StandardTruck(String licensePlate,String truckModel,int maxWeight) {
        super(licensePlate, truckModel);
        this.maxWeight= maxWeight;
        System.out.println("Creating "+ this.toString());
    }

因此,在创建 StandardTruck 实例或任何其他继承自 Truck 的实例后,变量 truckID 未被初始化或创建 正因为如此,每当我尝试对我创建的任何卡车做某事时,我总是得到最后创建的卡车 ID..

我是不是漏了什么??

truckID 初始化的,但由于是静态的,属于class,而不是个别实例,所以不显示在调试器上。

看来您需要另一个非静态字段truckID。但是字段不能有重名,所以我建议你将静态的重命名为 nextTruckID:

static private int nextTruckID = 2000;
private int truckID;

构造函数可以这样写:

public Truck() {
    this.licensePlate = randomNumber(100,999) + "-" + randomNumber(10,99) + "-" + randomNumber(100,999);
    this.truckModel= "M"+ randomNumber(0,4);
    truckID = nextTruckID++;
}

public Truck(String licensePlate, String truckModel) {
    this.licensePlate=licensePlate;
    this.truckModel=truckModel;
    truckID = nextTruckID++;
}

您的 truckID 不会继承给您的子class,因为它是静态的。为了获得增加的 truckID,您必须指定一个对象变量

private int truckId;

和一个class变量

private static int truckIdCounter = 2000; // 2000 is the start value

在你的卡车里 class。在 Truck class 的构造函数中,您可以为 Truck 实例设置 truckId 并增加静态卡车 ID:

this.truckId = truckIdCounter;
truckIdCounter++;

现在您应该能够在子class 实例中访问 truckId。

静态变量在单个 class 之间共享,这意味着您创建的任何实例都将具有相同的共享 truckId。您应该创建一个静态 lastTruckId 和一个私有实例变量 truckId,这样您就可以分配最新的 truckId 并增加 lastTruckId。 而且你不能继承私有变量,所以你可以创建一个新的 getter,使其成为 public,或者只是根据你的情况在抽象中保护 class.

试试这个:

abstract class Truck {
  private static int lastTruckId = 2000;

  public Truck() {
    lastTruckId++;
  }

  public static int getLastTruckId() {
    return lastTruckId;
  }
}

class StandardTruck extends Truck {
  private int truckId;

  public StandardTruck() {
    super();
    this.truckId = getLastTruckId();
  }

  public int getTruckId() {
    return truckId;
  }
}