Java程序设计案例教程 模块四 编程题1
医生有姓名(name)、编号(idnumber)、地址(address)等基本信息,医生又分为专业医生和非专业医生,他们所具有的信息和上面所列的基本信息一样,但是专业医生除了具有上述基本信息外还有特定信息——专业(speciality)。根据它们之间的关系,创建一个Doctor类,其中包含医生的基本信息,再利用既成创建一个非专业医生类NonSpecialist和专业医生类Specialist,并利用这些类的对象显示他们各自的信息。
public class Doctor {
//name idnumber address speciality
public String name;
public int idnumber;
public String address;
Doctor(String name,int idnumber,String address){
this.name=name;
this.idnumber=idnumber;
this.address=address;
}
public void show() {
System.out.println("姓名:"+name+"\t编号:"+idnumber+"\t地址:"+address);
}
}
class NonSpecialist extends Doctor{
public NonSpecialist(String name,int idnumber,String address) {
super(name,idnumber,address);
}
public void show() {
super.show();
}
}
class Specialist extends Doctor{
private String speciality;
public Specialist(String name,int idnumber,String address,String speciality) {
super(name,idnumber,address);
this.speciality=speciality;
}
public void show() {
System.out.println("姓名:"+name+"\t编号:"+idnumber+"\t地址:"+address+"\t专业:"+speciality);
}
}
public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
NonSpecialist n=new NonSpecialist("doc",1,"nc");
n.show();
Specialist s=new Specialist("doc",1,"nc","computer");
s.show();
}
}
医生有姓名(name)、编号(idnumber)、地址(address)等基本信息,医生又分为专业医生和非专业医生,他们所具有的信息和上面所列的基本信息一样,但是专业医生除了具有上述基本信息外还有特定信息——专业(speciality)。根据它们之间的关系,创建一个Doctor类,其中包含医生的基本信息,再利用既成创建一个非专业医生类NonSpecialist和专业医生类Specialist,并利用这些类的对象显示他们各自的信息。
public class Doctor {
//name idnumber address speciality
public String name;
public int idnumber;
public String address;
Doctor(String name,int idnumber,String address){
this.name=name;
this.idnumber=idnumber;
this.address=address;
}
public void show() {
System.out.println("姓名:"+name+"\t编号:"+idnumber+"\t地址:"+address);
}
}
class NonSpecialist extends Doctor{
public NonSpecialist(String name,int idnumber,String address) {
super(name,idnumber,address);
}
public void show() {
super.show();
}
}
class Specialist extends Doctor{
private String speciality;
public Specialist(String name,int idnumber,String address,String speciality) {
super(name,idnumber,address);
this.speciality=speciality;
}
public void show() {
System.out.println("姓名:"+name+"\t编号:"+idnumber+"\t地址:"+address+"\t专业:"+speciality);
}
}
public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
NonSpecialist n=new NonSpecialist("doc",1,"nc");
n.show();
Specialist s=new Specialist("doc",1,"nc","computer");
s.show();
}
}