本帖最后由 lwj123 于 2015-3-26 22:28 编辑
主题:final与类变量初始化问题【活动已结束】
奖励技术分数:1-4分
答题链接:技术答题之“final关键字【答题贴】http://bbs.itheima.com/thread-177845-1-1.html
- ---------------------------------------------程序1------------------------------------------------
- public class PriceTest {
- public static void main(String[] args) {
- //输出位置1
- System.out.println(Price.INSTANCE.currentPrice);
- Price p = new Price(2.8);
- //输出位置2
- System.out.println(p.currentPrice);
- }
- }
- class Price {
- public Price(double discount) {
- currentPrice = initPrie - discount;
- }
-
- static final Price INSTANCE = new Price(2.8);
-
- static double initPrie = 20;
- double currentPrice;
- }
- 问题一:请问程序1两个位置的输出一样吗?为什么?
- -------------------------------------------华丽分割线-----------------------------------
- ----------------------------------------------程序2--------------------------------
- public class PriceTest {
- public static void main(String[] args) {
- //输出位置1
- System.out.println(Price.INSTANCE.currentPrice);
- Price p = new Price(2.8);
- //输出位置2
- System.out.println(p.currentPrice);
- }
- }
- class Price {
- public Price(double discount) {
- currentPrice = initPrie - discount;
- }
-
- static final Price INSTANCE = new Price(2.8);
-
- static final double initPrie = 20;
- double currentPrice;
- }
- 问题二:请问程序2两个位置的输出一样吗?为什么?
复制代码
|
|