JAVA问题总结之21--private使用实例和手动设置报错
package java831;
public class testprivate {
public static void main(String[] args) {
person p= new person();
p.setAge(1120);
System.out.println(p.getAge());
}
}
class person{
private int age;
public void setAge(int i){
if(i>130 || i<0 ){
// System.out.println("您输入的有错");
throw new RuntimeException("您输入的有错");
}else{
age=i;
}
}
public int getAge(){
return age;
}
}
执行结果:
Exception in thread "main" java.lang.RuntimeException: 您输入的有错
at java831.person.setAge(testprivate.java:15)
at java831.testprivate.main(testprivate.java:6)
将setAge改为20后结果为:
20