class Price{
final static Price INSTANCE = new Price(2.8);
static double initPrice = 20;
double currentPrice;
public Price(double discount){
currentPrice = initPrice-discount;
}
}
class PriceTest{
public static void main(String arg[]){
System.out.println(Price.INSTANCE.currentPrice);
Price p = new Price(2.8);
System.out.println(p.currentPrice);
}
}
运行结果为什么不是两个17.2,求解释 作者: $love 时间: 2013-5-5 20:05
final static Price INSTANCE = new Price(2.8);
因为,经过static修饰的过的INSTANCE 对象只要类存在,这个对象就存在了,它存在后就会创建对象
就会调用相应的构造函数,故两个都是17.2