文档详情

第章类的封装、继承和多态.ppt

发布:2017-06-16约字共131页下载文档
文本预览下载声明
《Java程序设计实用教程(第3版)》 * public String getNumber() { return number; } public void setNumber(String number) { this.number = new String(number); } public String getSpeciality() { return speciality; } public void setSpeciality(String speciality) { this.speciality = new String(speciality); } public String toString(){//方法的覆盖 String str = super.toString()+,+number+,+speciality; return str; } public boolean equals(Student st){//方法的重载 return st==this||st!=null equals(new Person(st.getName(),st.getBirthday())) st.number.equals(number)st.speciality.equals(speciality); } } /*** * 拷贝构造方法,重载,复制对象 * @param p:用于构造的日期,Person类型 */ public Person(Person p) { this(p.name, p.birthday); } 《Java程序设计实用教程(第3版)》 * /* * 用户名设置 * @param name:需要设置成的用户名,String类型 */ public void setName(String name) { this.name = name; } /* *用户名获得 * * @return:用户名,String类型 */ public String getName() { return name; } public String toString(){ return name+,+birthday.toString(); } 《Java程序设计实用教程(第3版)》 * /*** * 获得生日 * @return 生日,MyDate类型 */ public MyDate getBirthday() { return birthday; } /** * 设置生日 * @param birthday,需要设置成的日期 */ public void setBirthday(MyDate birthday) { this.birthday = new MyDate(birthday); } 《Java程序设计实用教程(第3版)》 * public static void main(String args[]){ MyDate d = new MyDate(); String name = 李晓明; Person p1 = new Person(name, d); Person p2 = new Person(p1); //拷贝构造方法 System.out.println(p1:+p1+,p2:+p2+,p1==p2?+(p1==p2)+,p1.name==p2.name?+ (p1.name==p2.name)+,p1.birthday==p2.birthday?+(p1.birthday==p2.birthday)); d.setDate(2011, 1, 2); //p1.setName(wgw); System.out.println(p1:+p1+,p2:+p2); } } 《Java程序设计实用教程(第3版)》 * 《Java程序设计实用教程(第3版)》 * 3.2.6 浅拷贝与深拷贝 浅拷贝 /*** * 构造方法 * @param name:用于设置的用户名 * @p
显示全部
相似文档