class Father{
static void print(){
System.out.println ("in father method");
}
}
class Child extends Father{
static void print(){
System.out.println ("in child method");
}
}
class Test{
public static void main (String[] args) {
Father f =new Child();
f.print();
}
}
public class Test {
public static void main(String[] args) {
A a = new B(); //父类引用,子类对象; 同名成员变量父类无法引用,static方法也是如此。
System.out.println(a.i); //父类的变量
System.out.println(((B) a).i); //恢复到子类的变量
a.testMethod(); //调用子类方法,多态
a.testStaticMethod(); //调用父类方法。
((B)a).testStaticMethod();//恢复到子类引用调用子类方法,如同上边调用变量一样。
}
}
class A {
public int i = 1;
public void testMethod() {
System.out.println("this is in A");
}
public static void testStaticMethod() {
System.out.println("this is the static method in A");
}
}
class B extends A {
public int i = 2;
public int j = 0;
public void testMethod() {
System.out.println("this is in B");
}
public static void testStaticMethod() {
System.out.println("this is the static method in B");
}
}