当前位置: 代码迷 >> J2SE >> 小弟我的类参数传递,错在哪儿
  详细解决方案

小弟我的类参数传递,错在哪儿

热度:55   发布时间:2016-04-23 20:33:26.0
我的类参数传递,错在哪儿?
package prictice;

public class Student {
public static void main(String[] args) {
ClassMate stu1 = new ClassMate("小明",18,1);
System.out.println(stu1.showInfo());
stu1.setInfo("小明", 100, 101);
}

}

class ClassMate{
private String name;
private int age;
private int id;
public static int count = 0;
public ClassMate(){
count++;
}
public ClassMate(String name, int age, int id){
this.name = name;
this.age = age;
this.age = id;
}
public boolean setInfo(String name, int age, int id)
{
this.age = age;
this.name = name;
this.id = id;

return true;
}
public String showInfo(){
return "name="+this.name+" ,age="+this.age+" ,id="+this.id;
}
}


输出结果:name=小明 ,age=1 ,id=0
可是我明明在new一个对象的时候,传递的参数是:(小明,18,1)
年龄为什么是1,id又什么事0?
------解决方案--------------------
看看您的构造函数,id没有传给值,age传了两个值,后面的值把前面的覆盖了。
------解决方案--------------------
this.age=id
  相关解决方案