继承、接口小例子

来源:互联网 发布:super在java中的位置 编辑:程序博客网 时间:2024/06/05 04:54


package Doctors;

public abstract class Person {
 private String name;
 private int age;

 // 构造函数
 public Person(String myName, int myAge) {
  name = myName;
  age = myAge;
 }

 // 获取名字
 public String getName() {
  return this.name;
 }

 // 行走,子类可以不重写
 public void walk() {
  System.out.println("走两步!");
 }

 // 思考,抽象方法,子类必须对其进行重写
 public abstract void think();

}


package Doctors;

public class Doctor extends Person {
 private String salary;//属性
 private String hobby;//属性
 public Doctor(String mySalary, String myHobby,String myName, int

myAge){
  super(myName, myAge);//显示调用父类的构造函数
  salary=mySalary;
  hobby=myHobby;
 }
  public String getName(){
   return super.getName();
  }
  public String gethobby(){
   return this.hobby;
  }
  public void think() {
   System.out.println("我的职责是治病救人"); 
  }
  public void introduction(String specialintroduction){
   System.out.println(specialintroduction);
  }
  public void report(String specialreport){
   System.out.println("大家好,我最近研究的课题报告

是:"+specialreport);
  }
 }



package Doctors;


public class patients extends Person{
 private String disease;//属性
 private int severity;//属性

   
 public patients (String myDisease,int mySeverity,String myName, int

myAge){
  super(myName,myAge);//显示调用父类的构造函数
    disease=myDisease;
    severity=mySeverity;

    }//重写父类的抽象方法,病人生病后的感悟
  public void think(){
   System.out.println("\n"+this.getName()+":生病很痛苦,

切记加强身体锻炼,"+this.disease+"快点好起来!");
  }//病人接受的建议
  public void accept(){
   int mySeverity=this.severity;

   //用switch语句对随机数进行判断,然后输出话语
   switch(mySeverity){
   case 0:
    System.out.println("目前的"+this.disease+"病

情很轻,只需吃点药就好");
    break;
   case 1:
    System.out.println("目前的"+this.disease+"急

需帮助住院治疗");
    break;
   case 2:
    System.out.println("目前的"+this.disease+"危

及生命进入重症监护室");
    break;
   default:
    System.out.println("go to god");
   }
  }
 }



package Doctors;


public class bone_doctor extends Doctor implements opsdoctor{
 private String SpTreatment;//属性
 private String department;//属性
 public bone_doctor(String mySpTreatment, String mydepartment,String

mySalary, String myHobby,String myName, int myAge){
  super(mySalary, myHobby, myName, myAge);//调用父类的构造函数
  SpTreatment=mySpTreatment;
  department=mydepartment;
}//构造函数

@Override
//重写父类的方法
public String getName(){
 return super.getName();
}
public void operation(){
 int random=new java.util.Random().nextInt(3);
 if(random==0){
  System.out.println("我最拿手的是接骨");
 }else if (random==1){
  System.out.println("我最拿手的是关节炎治疗");
 }else if(random==2){
  System.out.println("我最拿手的是显微断指再植术");
 }else {
  System.out.println("我最拿手的是脊柱畸形矫正");
 }
}//接口
}



package Doctors;


public class eye_doctor extends Doctor implements opsdoctor{
 private String SpTreatment;//属性
 private String department;//属性
 public eye_doctor(String mySpTreatment, String mydepartment,String

mySalary, String myHobby,String myName, int myAge){
  super(mySalary, myHobby, myName, myAge);//调用父类的构造函数
  SpTreatment=mySpTreatment;
  department=mydepartment;
}//构造函数

@Override
//重写父类的方法
public String getName(){
 return super.getName();
}
public void operation(){
 int random=new java.util.Random().nextInt(3);
 if(random==0){
  System.out.println("我最拿手的是玻璃体视网膜手术");
 }else if (random==1){
  System.out.println("我最拿手的是准分子激光治疗");
 }else if(random==2){
  System.out.println("我最拿手的是斜视手术");
 }else {
  System.out.println("我最拿手的是近视手术");
 }

}//接口
}


package Doctors;

public class Pediatrician extends Doctor {
    private String treatmentobject;//治疗对象
   
    //构造函数
    public Pediatrician(String myTreatmentobject,String mySalary, String

myHobby,String myName, int myAge){
     super(mySalary,myHobby,myName,myAge);//调用父类构造函数  
     treatmentobject=myTreatmentobject;
    }
    //重写父类方法
    public String getName(){
     return super.getName();
    }
    //研究方向
    public void researchdirection(){
    System.out.println(this.getName()+"的研究方向是婴幼儿疾病预防");
    }
}


package Doctors;
//这是个接口
public interface opsdoctor {
 public void operation();
 
}



package Doctors;

public class doctortest {
 public static void main(String arg[]){
  //实例化五个医生
  bone_doctor bd1=new bone_doctor("接骨","骨科","六千","看书","

张三",28);
  bone_doctor bd2=new bone_doctor("骨性关节炎","骨科","八千","

看电影","李四",36);
  eye_doctor ed1=new eye_doctor("视力矫正","眼科","五千","打

球","王五",26);
  eye_doctor ed2=new eye_doctor("白内障手术","眼科","六千","购

物","赵六",35);
  Pediatrician pd=new Pediatrician("婴幼儿","六千","唱歌","田七

",29);
  //实例化五个病人
  patients pt1=new patients("全身性骨折",2,"小强",22);
  patients pt2=new patients("关节炎",1,"小蓝",29);
  patients pt3=new patients("近视",0,"小花",14);
  patients pt4=new patients("白内障",1,"小梅",65);
  patients pt5=new patients("幼儿感冒",0,"小宝",2);
  //医生亮相
  System.out.println("请各位医生给大家问个好~");
  System.out.println("-------------------------");
  bd1.introduction(bd1.getName()+":我最喜欢做的事情

是"+bd1.gethobby());
  bd2.introduction(bd2.getName()+":我最喜欢做的事情

是"+bd2.gethobby());
  ed1.introduction(ed1.getName()+":我最喜欢做的事情

是"+ed1.gethobby());
  ed2.introduction(ed2.getName()+":我最喜欢做的事情

是"+ed2.gethobby());
  pd.introduction(pd.getName()+":我最喜欢做的事情

是"+pd.gethobby());
  System.out.println("----------—---------------");
  //医生报告

  System.out.println("请各位医生进行报告~");
  System.out.println("--------------------------");
  System.out.println(bd1.getName()+"上台");
  bd1.operation(); 
  bd1.report("骨折手术报告");
 
  
  
  System.out.println("\n"+bd2.getName()+"上台"); 
  bd2.operation();
  bd2.report("关于关节炎的研究");
 

  
  System.out.println("\n"+ed1.getName()+"上台");
  ed1.operation();
  ed1.report("关于视力下降原因的研究");


  System.out.println("\n"+ed2.getName()+"上台"); 
  ed2.operation();
  ed2.report("关于白内障的手术创新");

  
  
  System.out.println("\n"+pd.getName()+"上台");
  pd.researchdirection();
  System.out.println("-------------------------");
  
  //病人
  System.out.println("接诊病人情况");
  System.out.println("-------------------------");
  System.out.println("骨科病人:");
  pt1.think();
        pt1.accept();

  pt2.think();
        pt2.accept();
       
  System.out.println("\n眼科病人:");
  pt3.think();
        pt3.accept();

  pt4.think();
        pt4.accept();
       
  System.out.println("\n儿科病人:");
  pt5.think();
        pt5.accept();
 }

}






0 0