声明Patient类表示门诊室中的病人,此类对象应包括name、sex、age、weight、allergies(boolean)。声明存取及修改方法。在一个单独的类中,声明测试方法,并生成两个patient对象,设置其状态并将其信息显示在屏幕上。声明并测试toString()方法显示一个病人的age、sex、name及allergies属性。
package com.lxl.test05;
public class Patient {
private String name;
private char sex;
private int age;
private double weight;
private boolean allergies;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public char getSex() {
return sex;
}
public void setSex(char sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public double getWeight() {
return weight;
}
public void setWeight(double weight) {
this.weight = weight;
}
public boolean isAllergies() {
return allergies;
}
public void setAllergies(boolean allergies) {
this.allergies = allergies;
}
@Override
public String toString() {
return "Patient [name="+name+",sex="+sex+",age="+age+
",weight="+weight+",allergies="+allergies+"]";
}
}
package com.lxl.test05;
public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
Patient p1 = new Patient();
Patient p2 = new Patient();
p1.setName("病号一");
p1.setSex('男');
p1.setAge(12);
p1.setWeight(120);
p1.setAllergies(true);
p2.setName("病号二");
p2.setSex('女');
p2.setAge(18);
p2.setWeight(160);
p2.setAllergies(false);
System.out.println(p1.getName());
System.out.println(p1.isAllergies());
System.out.println(p2.getName());
System.out.println(p2.isAllergies());
System.out.println(p1.toString());
System.out.println(p2.toString());
}
}