//在Java中,所有的类,都默认直接或者间接继承objec类
// Person 人 :父类
public class Person /*extends object*/ {
public Person() {
System.out.println("Person无参执行了");
}
protected String name = "wzy";
//私有的东西无法被继承
public void print(){
System.out.println("Person");
}
}
//学生 是 人 :派生类,子类
//子类继承父类,就会拥有父类的全部方法!
public class Student extends Person {
private String name = "lengleng";
public Student() {
//隐藏代码;默认调用了父类的无参构造
//若父类为有参,则可以 super(“name”);调用父类的有参
super(); //调用父类的构造器,必须要在子类构造器的第一行
System.out.println("Student有参执行了");
}
public void print() {
System.out.println("Student");
}
public void text1() {
print();
this.print();
super.print();
}
public void text(String name) {
System.out.println(name);
System.out.println(this.name);
System.out.println(super.name);
}
}
public class Application {
public static void main(String[] args) {
Student student = new Student();
//student.text("冷冷");
//student.text1();
}
}
super注意点:
1.super调用父类的构造方法,必须在构造方法的第一个
2.super 必须只能出现在子类的方法或者构造方法中!
3.super 和 this 不能同时调用构造方法
VS this
代表的对象不同:
this :本身调用者这个对象
super : 代表父类这个对象的应用
前提
this :没有继承也可以使用
super :只能在继承的条件下才可以使用
构造方法
this :本类的构造
super :父类的构造
