#最简单版本继承:
##父类
package base.oop.Demo05;
public class Person {
protected String name="父亲";
}
##子类
package base.oop.Demo05;
public class Student extends Person{
String name="学生";
public void test(String name) {
System.out.println(name);
System.out.println(this.name);
System.out.println(super.name);
}
}
##测试类
package base.oop.Demo05;
public class Application {
public static void main(String[] args) {
Student student = new Student();
student.test("学生方法形参");
}
}
#继承的方法调用
##父类2
package base.oop.Demo05;
public class Person {
public void print(){
System.out.println("父亲");
}}
##子类2
package base.oop.Demo05;
public class Student extends Person{
String name="学生";
public void print(){
System.out.println("学生");
}
public void test1(){
print();
this.print();
super.print();
}
}
##调用类2
package base.oop.Demo05;
public class Application {
public static void main(String[] args) {
Student student = new Student();
student.test1();
}
}
#继承类默认无参构造的调用先后顺序
##父类
package base.oop.Demo05;
public class Person {
public Person() {
System.out.println("父类的无参构造执行了");
}
}
##子类
package base.oop.Demo05;
public class Student extends Person{
String name="xuesheng";
public Student() {
/*super(),下面相当于有一个隐藏的代码,
调用父类的构造器就必须要在子类构造器的第一行
*/
//super()
System.out.println("子类的无参构造执行了");
}
}
##调用类
package base.oop.Demo05;
public class Application {
public static void main(String[] args) {
Student student = new Student();
}
}
#注意事项
1.调用父类的构造方法
2.必须在子类的构造方法的第一行
3.super只能出现在子类的方法中或者构造方法中
3.super和this不能同时调用构造方法
this与super本质的区别:
this:本身调用者这个对象
super:代表父类对象的引用
super与this前提的区别:
this:没有继承也可以使用
super:只能在继承条件下才可以使用
构造方法的区别:
this():本类的构造
super():父类的构造