//子类
package com.yuecheng.oop.Demo05;
//学生也是人(子类,派生类): 学生 is 人;
//子类继承父类,就会继承父类所有的方法
//注意:private(私有方法或属性) 私有的东西是无法继承的
public class Student extends Person{
//快捷键ctrl + h 打开树
public Student() {
// 这里隐藏了代码,调用了父类的无参构造
super();//调用父类构造器必须在子类的第一行
//上方的代码是默认的不写也行,这里只是知道原理
System.out.println("子类的无参构造执行了");
}
private String name = "婉茹";
public void print(){
System.out.println("Student");
}
public void test1( ){
print();//输出Student
this.print();//Student
super.print();//Person
}
public void test(String name){
System.out.println(name);//输出李白
System.out.println(this.name);//婉茹
System.out.println(super.name);//玥骋
}
}
package com.yuecheng.oop.Demo05;
//父类
public class Person {
public Person() {
System.out.println("父类Person无参构造执行了");
}
protected String name="玥骋";
public void print(){
System.out.println("Person");
}
}
package com.yuecheng.oop;
import com.yuecheng.oop.Demo05.Student;
public class Application {
public static void main(String[] args) {
Student student = new Student();
//student.test("李白");
student.test1();
}
}