变态的简单题
刚学完数据类型,练习题有这么一道题:
编写Ex3.java,实现下述功能并测试:
复制BirthDate类的对象(创建一个新对象,使之与原对象的各个属性值分别相等)
public BirthDate copy(BirthDate d){……}
必须要用规定方法,头都快想炸了怎么做都不符合题目。老师也说这样的题目没意义,就是把问题复杂化强化一下新手的编程思路。
其中BirthDate类的内容如下:
class BirthDate {
private int day;
private int month;
private int year;
public BirthDate(int d,int m,int y) {
day = d; month = m; year = y;
}
public void setDay(int d) {day = d;}
public void setMonth(int m) {month = m;}
public void setYear(int y) {year = y;}
public int getDay() {return day;}
public int getMonth() {return month;}
public int getYear() {return year;}
public void display() {
System.out.println(day + " - " + month + " - " + year);
}
}
public class Ex3{
public static void main(String[]args){
BirthDate bd = new BirthDate(22,12,2008);
......
}
public BirthDate copy(BirthDate d){
......
}
}
------解决方案--------------------这不是挺简单的吗?难道我理解错了?
public BirthDate copy(BirthDate d){
return new BirthDate(d.getDay(), d.getMonth(), d.getYear());
}
------解决方案--------------------就是克隆把,implement cloneable,然后覆盖object的clone方法,不就行勒
------解决方案--------------------都没错嘛