菜鸟求助
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
错在哪里了?要怎么改?
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
错在哪里了?要怎么改?
...全文


非技术区
