通过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);
    }
}



相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-08-03
  • 2021-06-11
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-02-08
  • 2021-08-30
  • 2021-07-29
  • 2021-06-01
  • 2021-04-19
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案