加入收藏 | 设为首页 | 会员中心 | 我要投稿 北几岛 (https://www.beijidao.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

继承编程练习

发布时间:2021-05-20 08:56:38 所属栏目:大数据 来源: https://blog.csdn.net/summoxj
导读:通过Son访问Father类: 代码如下: Father类: public class Father { ?? ?//实例变量与实例方法 ?? ?int xx=10; ?? ?public void method(){ ?? ??? ?System.out.println("in father,method"); ?? ?} ?? ? ?? ?//静态变量与静态方法 ?? ?static int yy=20; ??
通过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);
?? ?}
}



(编辑:北几岛)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读