JAVA调用另外一个构造器_类内调用另一个构造器的问题

类内调用另一个构造器的问题

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("s", ""); }

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 String getStudentBirthday()

{

return strBirthday;

}

public void setStudentBirthday(String birthday)

{

strBirthday = birthday;

}

public String getStudentSpeciality()

{

return strSpeciality;

}

public void setStudentSpeciality(String speciality)

{

strSpeciality = speciality;

}

public String getStudentAddress()

{

return strAddress;

}

public void setStudentAddress(String address)

{

strAddress = address;

}

public String toString()

{

String information = "学生姓名=" + strName + ", 学号=" + strNumber;

if( !strSex.equals("") )

information += ", 性别=" + strSex;

if( !strBirthday.equals(""))

information += ", 出生年月=" + strBirthday;

if( !strSpeciality.equals("") )

information += ", 专业=" + strSpeciality;

if( !strAddress.equals("") )

information += ", 籍贯=" + strAddress;

return information;

}

}

请问一下:类内用this调用另一个构造器,是不是要生成与被调用构造器方法参数相对应的一个新的对象?

如果是为什么用句柄tom.调用toString()输出的是名字是"s"呢?

明明初始化是命名为"Tom"了啊?

谢谢。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值