本帖最后由 刘文飞 于 2012-10-22 22:19 编辑
代码如下,问题见红色注释
————————————————————————————————————————
class Person{
private int age;
public Person(){}
public Person(int age){
this.age = age;
}
public void setAge(int age){
this.age = age;
}
public int getAge(){
return this.age;
}
public String toString(){
return "Age" + "\t" + this.age;
//return this.age;单独只是return this.age怎么就出现了类型转换错误了?前面加上字符串了就能完成自动转换?
}
}
public class ConstructionDemo01{
public static void main(String args[]){
Person per1 = new Person();//具有有参构造的时候覆写了默认的无参构造
per1.setAge(20);
System.out.println(per1);
}
}
|