理解构造代码块、静态代码块、构造方法、主方法的执行顺序class Test {private static String name;{System.out.print ("Hello");}static {System.out.print ("How");}Test(){System.out.print ("are");}public static void main(String[] args) {System.out.print("you");Test test = new Test();}}程序运行结果:
查看答案
方法重载,并理解int型与char关系class Test{int compute( int a,int b ){return b-a;}String compute(String s1,String s2){return s1+s2;}public static void main(String args[ ]){Test t=new Test ( );char ch1='a';char ch2='b';System.out.println(t.compute(ch1,ch2));}}程序运行结果
静态成员变量是共享的,非静态成员变量各对象独立class A {int n;static int m;public A(int n,int m) {this.n=n;this.m=m;}}class Test{public static void main(String args[ ]){A a1=new A(10,11);A a2=new A(20,21);System.out.println("a1:n="+a1.n+",m="+a1.m);System.out.println("a2:n="+a2.n+",m="+a2.m);}}程序运行结果:
接口回调的是子类重写的方法interface Person {double compute(float x,float y);}class Student implements Person {public double compute(float x,float y) {return x-y;}int compute(int x,int y) {return x*y;}}public class Test{public static void main(String args[]) {Person p=new Student();System.out.println( p.compute(30,10));}}程序运行结果:
非静态方法被子类重写,上转型对象调用的是子类重写的方法,静态方法被 子类重写,上转型对象调用的仍然是父类定义的静态方法。class SuperClass {void method() {System.out.println("SuperClass:method");}static void methodStatic() {System.out.println("SuperClass:methodStatic");}}class SubClass extends SuperClass {void method() {System.out.println("SubClass:method");}static void methodStatic() {System.out.println("SubClass:methodStatic");}}public class Test {public static void main(String[] args) {SuperClass a = new SubClass();a.method();a.methodStatic();}}程序运行结果: