实验3类和对象

声明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());		

	}



}

 

  • 6
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值