首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2ME开发 >

初学者有关问题,求哥哥姐姐们答疑上

2012-12-26 
菜鸟问题,求哥哥姐姐们答疑下~下面这段代码为什么输出的是日期的地址。不是日期呢?怎么解决啊、?class MyDat

菜鸟问题,求哥哥姐姐们答疑下~
下面这段代码为什么输出的是日期的地址。不是日期呢?怎么解决啊、?


class MyDate
{
int day,month,year;
MyDate()
{
day=1;
month=1;
year=2000;
}

MyDate(int day1,int month1,int year1)
{
day=day1;
month=month1;
year=year1;
}
class Employee
{
String name;
MyDate birthDate;
MyDate HireDate;
Employee()
{

}

Employee(String name1,MyDate birthDate1,MyDate HireDate1)
{
name=name1;
birthDate=birthDate1;
HireDate=HireDate1;
}

public String getDetail()
{
return "雇员姓名:"+name+" 出生日期为:"+birthDate+" 工作日期为:"+HireDate+"\n";
}

}

class Test2
{
public static void main (String args[])
{
MyDate bir=new MyDate(5,5,2002);

MyDate hir=new MyDate(2,3,2002);

Employee xiaoming=new Employee("小明",bir,hir);

System.out.print(xiaoming.getDetail());
}
}
[解决办法]
return "雇员姓名:"+name+" 出生日期为:"+birthDate+" 工作日期为:"+HireDate+"\n";
你返回的birthDate是MyDate的一个实例,你要得到日期肯定不可能了。
给MyDate类加上get、set方法 
return "雇员姓名:"+name+" 出生日期为:"+birthDate.getYear()+":"+birthDate.getMonth()+":"+birthDate.getDay()+" 工作日期为:"+HireDate+"\n";
或者直接重写toString方法。
也可以直接写成公有的属性,直接点属性

热点排行