分类: Java
2008-10-22 17:13:08
class SuperClass { SuperClass() { System.out.println("SuperClass constructor"); } } public class SubClass extends SuperClass { SubClass() { System.out.println("SubClass constructor"); } public static void main(String[] args) { SubClass sub = new SubClass(); } } 输出结果: SuperClass constructor SubClass constructor |
class SuperClass { SuperClass(String str) { System.out.println("Super with a string."); } } public class SubClass extends SuperClass { SubClass(String str) { System.out.println("Sub with a string."); } public static void main(String[] args) { SubClass sub = new SubClass("sub"); } } |
Super with a string. Sub with a string. |
class One { One(String str) { System.out.println(str); } } class Two { One one_1 = new One("one-1"); One one_2 = new One("one-2"); One one_3 = new One("one-3"); Two(String str) { System.out.println(str); } } public class Test { public static void main(String[] args) { System.out.println("Test main() start..."); Two two = new Two("two"); } } 输出结果: Test main() start... one-1 one-2 one-3 two |
class One { One(String str) { System.out.println(str); } } class Two { One one_1 = new One("one-1"); One one_2 = new One("one-2"); static One one_3 = new One("one-3"); Two(String str) { System.out.println(str); } } public class Test { public static void main(String[] args) { System.out.println("Test main() start..."); Two two_1 = new Two("two-1"); System.out.println("------------"); Two two_2 = new Two("two-2"); } } 输出结果: Test main() start... one-3 one-1 one-2 two-1 ------------ one-1 one-2 two-2 |
class One { One(String str) { System.out.println(str); } } class Two { One one_1 = new One("one-1"); One one_2 = new One("one-2"); static One one_3 = new One("one-3"); Two(String str) { System.out.println(str); } 3 } public class Test { static Two two_3 = new Two("two-3"); public static void main(String[] args) { System.out.println("Test main() start..."); Two two_1 = new Two("two-1"); System.out.println("------------"); Two two_2 = new Two("two-2"); } } 输出结果: one-3 one-1 one-2 two-3 Test main() start... one-1 one-2 two-1 ------------ one-1 one-2 two-2 |
class One { One(String str) { System.out.println(str); } } class Two { static int i = 0; One one_1 = new One("one-1"); static One one_2 = new One("one-2"); static One one_3 = new One("one-3"); Two(String str) { System.out.println(str); } } public class Test { public static void main(String[] args) { System.out.println("Test main() start..."); System.out.println("Two.i = " + Two.i); } } 4 输出结果: Test main() start... one-2 one-3 Two.i = 0 |