Java-toString的使用方法

List item

#代码自留

package exp1;

public class Patient {
	private String name;
	private char sex;
	private int age;
	private float weight;
	private boolean 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 isAllergies() {
		return allergies;
	}

/*使用toString()*/
	
	public Patient(String a, char b, int c, float d, boolean e) {
		// TODO Auto-generated constructor stub
		name = a;
		sex = b;
		age = c;
		weight = d;
		allergies = e;
	}

	public static void main(String[] args) {
		Patient april = new Patient("zhangli", 'f', 33, 154.72f, true);
		System.out.println("name: " + april.getName());
		System.out.println("sex: " + april.getSex());
		System.out.println("age: " + april.getAge());
		System.out.println("weight: " + april.getWeight());
		System.out.println("allergies: " + april.isAllergies());
	}
	
/*不使用toString()*/
/*
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Patient april = new Patient();
		april.setName("zhangli");
		april.setSex('f');
		april.setAge(33);
		april.setWeight(154.72f);
		april.setAllergies(true);
		System.out.println("name: " + april.getName());
		System.out.println("sex: " + april.getSex());
		System.out.println("age: " + april.getAge());
		System.out.println("weight: " + april.getWeight());
		System.out.println("allergies: " + april.isAllergies());
	}
*/
}


上一篇:KMP算法入门


下一篇:ECS34 SQ19