通过Son访问Father类:
代码如下:
Father类:
public class Father {
//实例变量与实例方法
int xx=10;
public void method(){
System.out.println("in father,method");
}
//静态变量与静态方法
static int yy=20;
public static void sMethod(){
System.out.println("in father,static method");
}
}
Son类:
public class Son extends Father {
}
测试Demo类:
public class Demo {
public static void main(String[] args) {
//通过子类对象访问从父类继承下来的实例成员
Son xiaoMing=new Son();
xiaoMing.method();
System.out.println(xiaoMing.xx);
//通过子类名访问父类的静态成员
Son.sMethod();
System.out.println(Son.yy);
}
}