本帖最后由 陆强强 于 2012-6-19 10:34 编辑
class Student
{
private String name;
private int age;
private int results;
Student(String name,int age,int results)
{
this.name=name;
this.age=age;
this.results=results;
}
public void setName(String name)
{
this.name=name;
}
public String getName()
{
return name;
}
public void setAge(int age)
{
if(age>0)
this.age=age;
else
System.out.println("年龄输入错误");
}
public int getAge()
{
return age;
}
public void setResults(int results)
{
if(results>0)
this.results=results;
else
System.out.println("成绩输入错误");
}
public int getResults()
{
return results;
}
public void speak()
{
System.out.println(name+","+age+","+results);
}
}
class Study
{
public static void main(String[] args)
{
new Student("王五",17,-78).speak();
}
}
要求;通过new Student("王五",17,-78)传入年龄和成绩为负数时打印出如上的("成绩输入错误")等,而不是负数的值,就是说我通过SET方法传入负数成绩,打印("成绩输入错误")没问题,但用new Student("王五",17,-78)传入时依旧显示负数成绩
|
|