所以我尝试在网上进行研究,我觉得它是我的 toString(),但它在某种程度上是正确的,我想不出任何其他方式。
我希望能够输出:Lexie-高度:2.6 英尺。出生日期:2009 年 11 月 5 日
我的代码如下所示:
import java.util.*;
class Kid {
String name;
double height;
GregorianCalendar bDay;
public Kid () {
this.name = "HEAD";
this.height = 1;
this.bDay = new GregorianCalendar(1111,1,1);
}
public Kid (String n, double h, String date) {
StringTokenizer st = new StringTokenizer(date, "/", true);
n = this.name;
h = this.height;
}
/**
public String toString() {
return (this.name + ", Height: " + this.height + "ft., Born: "
+ this.month + "/" + this.day + "/" + this.year);
}
*/
public String toString() {
Calendar currentDate = Calendar.getInstance();
return (this.name + ", Height: " + this.height + "ft., Born: "
+ this.bDay);
}
} //end class
调用 Kid 类如下
class Driver {
public static void main (String[] args) {
Kid kid1 = new Kid("Lexie", 2.6, "11/5/2009");
System.out.println(kid1.toString());
} //end main method
} //end class