Topic: STATIC的问题 |
Print this page |
1.STATIC的问题 | Copy to clipboard |
Posted by: kingmx Posted on: 2004-09-21 15:13 import com.bruceeckel.simpletest.*; class Cup { Cup(int marker) { System.out.println("Cup(" + marker + ")"); } void f(int marker) { System.out.println("f(" + marker + ")"); } } class Cups { static Cup c1; static Cup c2; static { c1 = new Cup(1); c2 = new Cup(2); } Cups() { System.out.println("Cups()"); } } public class ExplicitStatic { static Test monitor = new Test(); public static void main(String[] args) { System.out.println("Inside main()"); Cups.c1.f(99); // (1) monitor.expect(new String[] { "Inside main()", "Cup(1)", "Cup(2)", "f(99)" }); } // static Cups x = new Cups(); // (2) // static Cups y = new Cups(); // (2) } ///:~ 为什么会打印出Cup(2)呀 |
2.Re:STATIC的问题 [Re: kingmx] | Copy to clipboard |
Posted by: alpacino Posted on: 2004-09-23 16:03 c1,c2是2个不同的静态对象, 因为c1,c2是在内存中指向2个不同的地址,所以他们不能互相干扰; 如果把c2改成c1,结果还是一样,因为你初始化了2遍,第一遍初始化的值在第二遍初始化时被清空,所以结果还是一样。 |
Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1 客服电话 18559299278 客服信箱 714923@qq.com 客服QQ 714923 |