我想知道为什么每次调用该方法时我的 policyNumber 都不会增加 1。当我运行它时,我得到了这个。
我试图让我添加的每个策略自动成为 PolicyNumber++。相反,我得到了相同的数字。相反,相同的值被分配给我制定的所有新策略。
run:
Policy #3 with amount $320.0
Policy #3 with amount $500.14
Policy #3 with amount $0.0
false
我以前使用过这种方法,我不明白为什么它现在不起作用。有人可以阐明一下吗?谢谢!
主班
public class Assignment3 {
public static void main(String[] args) {
Policy p1 = new Policy(320);
Policy p2 = new Policy(500.14);
Policy p3 = new Policy(0);
System.out.println(p2.policyNumber);
p1.print(); System.out.println();
p2.print(); System.out.println();
p3.print(); System.out.println();
System.out.println(p1.isExpired());
}
}
策略类
public class Policy {
double amount;
public int policyCount;
public static int policyNumber;
public Policy(){
amount = 0;
policyCount = 1;
policyNumber=0;
}
public Policy(double a){
amount = a;
policyNumber ++;
}
public boolean isExpired(){
return false;
}
public void print(){
System.out.print("Policy #" + this.policyNumber + " with amount $" + this.amount);
}
}