医生继承
public class Demo {
public static void main(String[] args) {
Doctor doctor=new Doctor();
doctor.zuoshoushu();
doctor.eat();
Nurse nurse=new Nurse();
nurse.takeCare();
nurse.eat();
}
}
public class Doctor extends Person{
public void zuoshoushu(){
System.out.println("医生在做手术");
}
}
public class Nurse extends Person{
public void takeCare(){
System.out.println("护士正在照顾病人");
}
}
public class Person {
private String name;
private int age;
public void eat(){
System.out.println("医生正在吃饭");
}
舅舅继承
public class Uncle {
private String name;
private int age;
//getter setter
public void setName(String name){
this.name=name;
}
public String getName(){
return name;
}
public void setAge(int age){
this.age=age;
}
public int getAge(){
return age;
}
public void sendHongbao(){
System.out.println("舅舅再发红包");
}
public Uncle(){
System.out.println("父类的无参构造方法被调用了,父类被创建了");
}
public class UncleOne extends Uncle {
public UncleOne(){
super();//调用父类的构造方法
System.out.println("子类大舅的构造方法被创建了");
}
public void chouyan() {
System.out.println("大舅喜欢抽烟");
}
@Override
public void sendHongbao(){
System.out.println("大舅家道中落红包只有一毛");
}
}
//多层继承
public class UncleOneSon extends UncleOne{
}
public class UncleTwo {
public void drinkWine(){
System.out.println("二舅喜欢喝酒");
}
}
public class Demo {
public static void main(String[] args) {
UncleOne yuqian=new UncleOne();
yuqian.sendHongbao();
yuqian.chouyan();
/*UncleTwo uncleTwo=new UncleTwo();
uncleTwo.drinkWine();
uncleTwo.drinkWine();*/
final Uncle uncle1=new Uncle();
//uncle1=new Uncle();
uncle1.setName("修改前的名字");
System.out.println(uncle1.getName());
uncle1.setName("修改后的名字");
System.out.println(uncle1.getName());
}
}