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
版权声明:本文为博主原创文章,未经博主允许不得转载。
时间: 2024-10-14 14:08:38