只能得到地址的值。
------------------------------------
/*
*学生类的重载
*/public class StudentOverloading
{
public static void main(String[] args)
{
Student tom = new Student("Tom");
tom.setStudentSex("man");
tom.setStudentAddress("America");
System.out.println(tom.toString());
}
}class Student
{
private String strName = "";
private String strNumber = "";
private String strSex = "";
private String strBirthday = "";
private String strSpeciality = "";
private String strAddress = "";
public Student()
{}
public Student(String name)
{
this(name, "");
}
public Student(String name, String number)
{
strName = name;
strNumber = number;
}
public String getStudentName()
{
return strName;
}
public String getStudentNumber()
{
return strNumber;
}
public void setStudentSex(String sex)
{
strSex = sex;
}
public String getStudentSex()
{
return strSex;
}
public void setStudentBirthday(String birthday)
{
strBirthday = birthday;
}
public String getStudentBirthday()
{
return strBirthday;
}
public void setStudentSpeciality(String speciality)
{
strSpeciality = speciality;
}
public String getStudentSpeciality()
{
return strSpeciality;
}
public void setStudentAddress(String address)
{
strAddress = address;
}
public String getStudentAddress()
{
return strAddress;
}
public String toString()
{
String information = "姓名:" + strName + ", 学号:" + strNumber;
if(!strSex.equals(""))
information = "性别:" + strSex;
if(!strSpeciality.equals(""))
information = "专业:" + strSpeciality;
if(!strBirthday.equals(""))
information = "出生日期:" + strBirthday;
if(!strAddress.equals(""))
information = "地址:" + strAddress;
return information;
}
}
------------------------------------
/*
*学生类的重载
*/public class StudentOverloading
{
public static void main(String[] args)
{
Student tom = new Student("Tom");
tom.setStudentSex("man");
tom.setStudentAddress("America");
System.out.println(tom.toString());
}
}class Student
{
private String strName = "";
private String strNumber = "";
private String strSex = "";
private String strBirthday = "";
private String strSpeciality = "";
private String strAddress = "";
public Student()
{}
public Student(String name)
{
this(name, "");
}
public Student(String name, String number)
{
strName = name;
strNumber = number;
}
public String getStudentName()
{
return strName;
}
public String getStudentNumber()
{
return strNumber;
}
public void setStudentSex(String sex)
{
strSex = sex;
}
public String getStudentSex()
{
return strSex;
}
public void setStudentBirthday(String birthday)
{
strBirthday = birthday;
}
public String getStudentBirthday()
{
return strBirthday;
}
public void setStudentSpeciality(String speciality)
{
strSpeciality = speciality;
}
public String getStudentSpeciality()
{
return strSpeciality;
}
public void setStudentAddress(String address)
{
strAddress = address;
}
public String getStudentAddress()
{
return strAddress;
}
public String toString()
{
String information = "姓名:" + strName + ", 学号:" + strNumber;
if(!strSex.equals(""))
information = "性别:" + strSex;
if(!strSpeciality.equals(""))
information = "专业:" + strSpeciality;
if(!strBirthday.equals(""))
information = "出生日期:" + strBirthday;
if(!strAddress.equals(""))
information = "地址:" + strAddress;
return information;
}
}
===
{
String information = "姓名:" + strName + ", 学号:" + strNumber;
if(!strSex.equals(""))
information = "性别:" + strSex;
if(!strSpeciality.equals(""))
information = "专业:" + strSpeciality;
if(!strBirthday.equals(""))
information = "出生日期:" + strBirthday;
if(!strAddress.equals(""))
information = "地址:" + strAddress;
return information;
}
上述方法写的有问题~~~
可以改为如下~~ public String toString()
{
String information = "姓名:" + strName + ", 学号:" + strNumber;
if(!strSex.equals(""))
information = information + ", 性别:" + strSex;
if(!strSpeciality.equals(""))
information = information + ", 专业:" + strSpeciality;
if(!strBirthday.equals(""))
information = information + ", 出生日期:" + strBirthday;
if(!strAddress.equals(""))
information = information + ", 地址:" + strAddress;
return information;
}
information =information + "性别:" + strSex;
if(!strSpeciality.equals(""))
information =information + "专业:" + strSpeciality;
if(!strBirthday.equals(""))
information =information + "出生日期:" + strBirthday;
if(!strAddress.equals(""))
information =information + "地址:" + strAddress;