luohzzz

导航

extend、super、this

![在这里插入图片描述](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();
}
}

posted on 2021-05-01 18:48  luohzzz  阅读(81)  评论(0编辑  收藏  举报