有趣的java程序
定义People类:
public class People { private String name; private String sex; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } }
定义Hospital类:
public class Hospital { private People a; public People getPeople() { return a; } public void setPeople(People a) { this.a = a; } public void changePeopleInfo(){ a.setName("小青"); a.setSex("女"); } }
main方法:
public static void main(String[] args) { People a = new People(); a.setName("张三"); a.setSex("男"); Hispital b =new Hispital(); b.setA(a); b.setAInfo(); System.out.println(a.getName()+"--"+a.getSex()); }
程序解读:
一个人姓名:张三,性别:男; 把这个人放进医院,改造后变为小青,性别:女.