JAVA面向对象继承 及super的学习

Super 的注意点:
1.Super调用父类的构造方法,必须在构造方法的第一个
2.Super只能出现在子类的方法或者构造方法中
3.Super和 this 不能同时调用构造方法


Vs this :
代表的对象不同:
this: 本身调用者这个对象
super: 代表父类对象的应用
前提:
this: 没有继承也可以使用
super: 只有在继承条件下才可以使用
构造方法:
this: 本类构造
super: 父类构

 

//Person is 人 : 父类
//在Java中所有的类都继承Object类
public class Person {
//public 公共的
//protected 受保护的
//default 默认的
//private 私有的
private int money= 10_0000_0000;

public int getMoney() {
return money;
}

public void setMoney(int money) {
this.money = money;
}

public void say(){
System.out.println("说了一句话");

}
protected String name = "敖";
public Person(){
System.out.println("父类输出");
}
}



//学生 is 人  派生类 ,子类
//子类继承了父类就会拥有父类的全部方法!
public class Student extends Person{
public static void main(String[] args) {
Student student = new Student();

//student.test("敖晨光");

}
private String name = "吖";
public void test(String name ){
System.out.println(name);//方法,依靠系统输入
System.out.println(this.name);//当前类里面的东西
System.out.println(super.name);//父类里面的东西
}
public Student(){
//隐藏代码调用了父类的无参构造
super();//调用父类的构造器必须要在子类的第一行
System.out.println("子类输出");
}

public Student(String name) {
this.name = name;
}


}
 
posted @ 2020-07-06 23:25  光光1234  阅读(112)  评论(0编辑  收藏  举报