package com.oop.demo05;
public class Person {
protected String name = "epfox2";
public void print(){
System.out.println("epfox2");
}
public Person(){
System.out.println("这是父类无参构造执行");
}
public Person(String name){
System.out.println("这是父类");
}
}
package com.oop.demo05;
public class Student extends Person{
protected String name = "epfox1";
public void test(String name){
System.out.println(name);
System.out.println(this.name);
System.out.println(super.name);
}
public void print(){
System.out.println("epfox1");
}
public void test2(){
print();
this.print();
super.print();
}
public Student(){
super();
System.out.println("这是子类无参构造执行");
}
public Student(String name) {
this.name = name;
}
public Student(String name, String name1) {
super(name);
this.name = name1;
}
}
package com.oop.demo05;
public class Application {
public static void main(String[] args) {
Student student = new Student();
}
}
super注意点:
1、super调用父类的构造方法,必须在构造方法的第一个
2、super必须只能出现在子类的方法或者构造方法中
3、super和this不能同时调用构造方法
super和this的区别:
代表的对象不同:
this:本身调用者这个对象
super:代表父类对象的应用
前提:
this:没有继承也可以使用
super:只能在继承条件下才可以使用
构造方法:
this():本类的构造
super():父类的构造