public class TestToString {
String name;
int age;
public static void main(String[] args) {
TestToString tts = new TestToString("hello",15);
TestToString(String s , int i) { TestToString(String s , int i) {
this.name = s; this.name = name;
public String toString() {
return name+" "+age;
}
}
String name;
int age;
public static void main(String[] args) {
TestToString tts = new TestToString("hello",15);
System.out.println(tts);
如果没有重写toString 方法,那么tts 是对象类型;只能调用Object的toString方法;
输出的结果就是包名加类名加@加上哈希码;
TestToString(String s , int i) { TestToString(String s , int i) {
this.name = s; this.name = name;
this.age = i; this.age = age
//出错的地方 : 参数没有注意传递正确的参数ming
}public String toString() {
return name+" "+age;
}
}