请问为什么自动生成的return语句在控制台没有任何输出,而使用System.out,println则能正常输出
packagecom.suse.inject.constructor;publicclassUser{privateStringusername;privateintag...
package com.suse.inject.constructor;
public class User {
private String username;
private int age;
private int money;
public User() {
super();
}
public User(String username, int age, int money) {
super();
this.username = username;
this.age = age;
this.money = money;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getMoney() {
return money;
}
public void setMoney(int money) {
this.money = money;
}
@Override
public String toString() {
return "User [username=" + username + ", age=" + age + ", money="
+ money + "]";
System.out.println("[username=" + username + ", age=" + age + ", money="
+ money + "]");
}
} 展开
public class User {
private String username;
private int age;
private int money;
public User() {
super();
}
public User(String username, int age, int money) {
super();
this.username = username;
this.age = age;
this.money = money;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getMoney() {
return money;
}
public void setMoney(int money) {
this.money = money;
}
@Override
public String toString() {
return "User [username=" + username + ", age=" + age + ", money="
+ money + "]";
System.out.println("[username=" + username + ", age=" + age + ", money="
+ money + "]");
}
} 展开
2个回答
2015-06-23
展开全部
return只是程序结束时,对上一层方法的一种反馈,当你调用toString方法,结束返回到调用它的方法的代码段继续执行,这个行为可以带着一个值,这个就是返回值。你可以这样System.out.println(new User().toString()); 这样就能输出return的字符串了。pringln方法参数是String,如果传入参数是一个非基本类型的对象,就会隐式地调用它的toString方法,所以new User()后面这个.toString()可以省略。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询