[英]How can i compare value staffNo of different class and how do i know if a list exists in java
我有三個不同的員工 class。 第一個是牙醫 class,第二個是護士 class,第三個是接待員 class,我有一個工作人員 ZA2F2ED4F8EBC2CBB4C21 繼承了其他類
關於牙醫 class 的示例代碼:
public class Dentist extends Staff {
private final int GDCRegNo = randInt(1000000, 9999999);
private List<Patient> patients;
private List<Dentist> dentists;
public Dentist(String name) {
this.name = name;
}
public static int randInt(int min, int max) {
List<Dentist> dentists;
Random rand = new Random();
int randomNum = rand.nextInt((max - min) + 1) + min;
return randomNum;
}
}
和關於員工 class 的示例代碼:
public class Staff {
protected int staffNo = randInt(1000,9999);
protected String name;
private int getStaffNo() {
return this.staffNo;
}
private String getName() {
return this.name;
}
public static int randInt(int min, int max) {
List<Dentist> dentists;
Random rand = new Random();
int randomNum = rand.nextInt((max - min) + 1) + min;
return randomNum;
}
}
I want to set all different staffNo of object of class(staffNo of dentist class, nurse class and receptionist class) unique with lists and I want to set all GDCRegNo of dentist object unique with lists
我怎樣才能做到這一點?
我想如果我能得到所有數量的 class 並比較它們,我可以解決它但是如何解決?
您可以在Staff
class 中添加一個 static 字段,以幫助您在新的 object 實例化時生成新的唯一 ID
public class Staff {
// last used id when instantiate new object
protected static int lastId = 0;
protected int staffNo;
protected String name;
public Staff() {
this.staffNo = lastId;
// increment lastId, so next instance will have different id
lastId++;
}
}
在子類中只需調用超級構造函數:
public class Dentist extends Staff {
public Dentist(String name) {
super();
this.name = name;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.