3条回答 默认 最新
- 咕泡-文泰 2021-07-19 04:21关注
public class Person {
public static void main(String[] args) throws Exception { Person person = new Person("张三","男",20); System.out.println(person) ; } private String name = ""; protected String gradel=""; int age = 0; public Person() { } public Person(String name, String gradel, int age) throws Exception { if (!("男".equals(gradel) || "女".equals(gradel))) { throw new Exception("性别只能是男或者女"); } if (age < 0 || age > 130) { throw new Exception("年龄超过范围"); } this.name = name; this.gradel = gradel; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getGradel() { return gradel; } public void setGradel(String gradel) { this.gradel = gradel; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public String toString() { return "Person{" + "name='" + name + '\'' + ", gradel='" + gradel + '\'' + ", age=" + age + '}'; }
}
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 2无用 3