有趣的java-大家伙儿一起来猜猜看结果?(如果你有结果请给出你的解释)
有趣的java-大家一起来猜猜看结果?(如果你有结果请给出你的解释)
案例一:
案例二:
案例三:
案例一:
class Test { private static Test test = new Test(); public static int counter1; public static int counter2 = 0; public Test() { counter1++; counter2++; } public static Test getInstance() { return test; } } public class ClassLoadTest { public static void main(String[] args) { Test test = Test.getInstance(); System.out.println("counter1\t"+Test.counter1); System.out.println("counter2\t"+Test.counter2); } }
案例二:
class Test { public static int counter1; public static int counter2 = 0; private static Test test = new Test(); public Test() { counter1++; counter2++; } public static Test getInstance() { return test; } } public class ClassLoadTest { public static void main(String[] args) { Test test = Test.getInstance(); System.out.println("counter1\t"+Test.counter1); System.out.println("counter2\t"+Test.counter2); } }
案例三:
abstract class A{ public A(){ this.print() ; } public abstract void print() ; }; class B extends A{ private int x = 100 ; public B(int x){ this.x = x ; } public void print(){ System.out.println("x = " + x) ; } }; public class TestJava{ public static void main(String args[]){ A a = new B(10) ; } };