- public class Students {
- private String name;
- private int age;
- private double testScore;
- Students() {
- }
- Students(String name, int age, double testScore) {
- this.name = name;
- if (age > 0 && age < 120) {
- this.age = age;
- } else
- System.out.println("非法年龄");
- if (testScore > 0 && testScore < 150) {
- this.testScore = testScore;
- } else
- System.out.println("非法成绩");
- }
- void show() {
- System.out.println("姓名:" + name);
- System.out.println("年龄:" + age);
- System.out.println("考试成绩:" + testScore);
- }
- public String getNaem() {
- return name;
- }
- public void setNaem(String naem) {
- this.name = name;
- }
- public int getAge() {
- return age;
- }
- public void setAge(int age) {
- if (age > 0 && age < 120) {
- this.age = age;
- } else
- System.out.println("非法年龄");
- }
- public double getTestScore() {
- return testScore;
- }
- public void setTestScore(double testScore) {
- if (testScore > 0 && testScore < 150) {
- this.testScore = testScore;
- } else
- System.out.println("非法成绩");
- }
- }
复制代码 代码在这,传入非法年龄在构造函数里的时候年龄就成0了;
|
|