声明Patient类表示在门诊室中的病人。
(1)此类对象应该包括name(String)、sex(char)、age(int)、weight(float)、allergies(boolean)。
(2)这些属性只能被该类访问。
(3)在该类中声明存取及修改方法。
(4)该类中至少提供一个构造函数,例如:public Patient(String name),其中构造函数的参数是name。
(5)在一个单独的TestPatient类中,声明测试方法,并生成两个patient对象,设置其状态并将信息显示在屏幕上。
(6)声明并测试toString()方法,在该方法中显示该病人的全部属性信息。
下面是测试一个patient的例子。
Patient p1=new Patient(“zhangsan”);
p1.setSex(‘f’);
p1.setAge(18);
p1.setWeight(100.00f);
p1.setAllergies(false);
System.out.println(“name=” + p1.getName());
System.out.println(“sex=” + p1.getSex());
System.out.println(“age=” + p1.getAge());
System.out.println(“weight=” + p1.getWeight());
System.out.println(“allergies=” + p1.getAllergies());
System.out.println(p1.toString());
老师的
//Patient.java
public class Patient {
private String name;
private char sex;
private int age;
private float weight;
private boolean allergies;
public Patient(String name){
this.name=name;
}
public Patient(String name,char sex){
this.name=name;
this.sex =sex;
}
public Patient(String name,char sex,int age){
this(name,sex);
this.age=age;
}
public Patient(String name,char sex,int age,float weight){
this(name,sex,age);
this.weight=weight;
}
public Patient(String name,char sex,int age,float weight,boolean allergies){
this(name,sex,age,weight);
this.allergies=allergies;
}
public void setName(String name){
this.name=name;
}
public void setSex(char sex){
this.sex=sex;
}
public void setAge(int Age){
this.age=age;
}
public void setWeight(float weight){
this.weight=weight;
}
public void setAllergies(boolean allergies){
this.allergies=allergies;
}
public String getName(){
return name;
}
public char getSex(){
return sex;
}
public int getAge(){
return age;
}
public float getWeight(){
return weight;
}
public boolean getAllergies(){
return allergies;
}
public String toString(){
String strRtn="Name=" + name + " sex=" + sex + " age=" + " weight=" + weight + " allergies=" + allergies;
return strRtn;
}
}
//TestPatient.java
public class TestPatient {
public static void main(String[] args){
Patient p1=new Patient("zhangsan");
p1.setSex('f');
p1.setAge(18);
p1.setWeight(100.00f);
p1.setAllergies(false);
System.out.println("name=" + p1.getName());
System.out.println("sex=" + p1.getSex());
System.out.println("age=" + p1.getAge());
System.out.println("weight=" + p1.getWeight());
System.out.println("allergies=" + p1.getAllergies());
System.out.println(p1.toString());
Patient p2=new Patient("lisi",'m',20,120.00f,true);
//p2.setSex('m');
//p2.setAge(20);
//p2.setWeight(120.00f);
//p2.setAllergies(true);
System.out.println("name=" + p2.getName());
System.out.println("sex=" + p2.getSex());
System.out.println("age=" + p2.getAge());
System.out.println("weight=" + p2.getWeight());
System.out.println("allergies=" + p2.getAllergies());
System.out.println(p2.toString());
}
}
版权声明:本文为zhaoshiwei66原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。