public class ClassDemo {
public static void main(String[] args) {
Person p = new Person();
p.setAge(153);
p.speak();
}
}
class Person {
Person() {
System.out.println("你好");
}
private int age;
public void setAge(int age) {
if (age > 150 || age < 0) {
throw new RuntimeException("非法年龄");
}
this.age = age;
}
public void speak(
) {
System.out.println("age"+age);
}
}
为什么会出现先抛出异常,然后再构造函数打印
|
|