继承
主程序:
public class Application {
public static void main(String[] args) {
Student student = new Student();
// student.test("三毛");
//student.testmethod();
}
}
父类:
// Person 人
public class Person {
public Person(){
System.out.println("Peron无参构造执行了");
}
protected String name = "sanwin";
public void print(){
System.out.println("Person");
}
}
子类:
// Student is 人:派生类或子类
public class Student extends Person{
public Student(){
//隐藏代码:调用父类的无参构造
super();//调用父类的构造器,必须要放在子类的第一行
System.out.println("Student的无参构造执行了");
}
protected String name = "Frank";
public void print(){
System.out.println("Student");
}
public void test(String name){
System.out.println(name);
System.out.println(this.name);
System.out.println(super.name);
}
//私有的不能被继承。
public void testmethod(){
print(); //调用当前的
this.print(); //调用当前类的
super.print(); //调用父类的
}
}
super注意点:
1.super调用父类的构造方法,必须在构造方法的第一个
2.super必须只能出现在子类的方法或者构造方法中
3.super和this不能同时调用构造方法
VS this;
代表的对象不同:
this: 本身调用者这个对象
super:代表父类对象的应用
前提
this:没有继承也可以使用
super:只能在继承条件下才可以使用
构造方法
this();本类的构造
super():父类的构造!
浙公网安备 33010602011771号