首页 > 其他 > 详细

extend、super、this

时间:2021-05-01 20:56:36      阅读:22      评论:0      收藏:0      [点我收藏+]

![在这里插入图片描述](https://img-blog.csdnimg.cn/img_convert/5995528bc6bb509159c238fa8282aa35.png#pic_center)
~~~java
//Person10就是父类

public class Person10 {
//在JAVA中,所有的类都默认extend object类


public Person10() {
System.out.println("先执行父类无参");//1
}

public void say(){
System.out.println("罗是好人");
}
//super
protected String name = "罗汉照";

public void print(){
System.out.println("Person");
}


}

---------------------------------------------------------
//Person10就是父类,Student10就是子类

public class Student10 extends Person10{//子类extends父类的全部的方法


public Student10() {//super调用父类构造器,必须要在子类构造器的第一行
super();
System.out.println("子类无参调用了父类无参,所以先执行父类的无参");//2
//存在隐藏代码:super()
}

private String name = "罗汉";
public void print(){
System.out.println("Student");
}

public void test1(){
print();//7
this.print();//7
super.print();//8
}

public void test(String name){
System.out.println(name);//4
System.out.println(this.name);//5
System.out.println(super.name);//6
}

 


}

----------------------------------------------------------
public class oopDemo10 {
public static void main(String[] args) {

Student10 Student10 = new Student10();
Student10.say();//3

Student10.test("罗");
Student10.test1();
}
}

extend、super、this

原文:https://www.cnblogs.com/luohzz/p/14724604.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!