+ -
当前位置:首页 → 问答吧 → 菜鸟求助!!

菜鸟求助!!

时间:2011-11-08

来源:互联网

package cn.guangzhou.xp;

class Person {

private int age;
private int x;

public Person(int age){

age = x;
}

public Person() {

}

public void setAge(int x) {

if (age <= 0) {
return;
} else {
age = x;
}
}

public int getAge() {
return age;
}

void shout() {
System.out.println("My age is " + age);
}

public static void main(String[] args) {

Person p1 = new Person();
Person p2 = new Person();

p1.setAge(5);
p1.shout();

p2.age = -1;
p2.shout();

}

}

输出结果为什么是 0跟-1 而不是5跟0
错在哪里了??怎么改

作者: JavaLittleStudent   发布时间: 2011-11-08

我的个神..你的代码也太坑人了...
 
你在setAge的方法里.if条件判断里是age <= 0 所以p1.setAge(5)的时候age还是0呀.所以...怎么可能会是5跟0.....

作者: yuan243197953   发布时间: 2011-11-09

热门下载

更多