1 public class Test3 2 { 3 public static void main(String[] args) 4 { 5 Student s1=new Student(10,"貂蝉",18); 6 Student s2=new Student(11,"西施",17); 7 Student s3=new Student(-12,"刘欢",-18); 8 s2.setNumber(-10); 9 System.out.println(s1.getInfo()); 10 System.out.println(s2.getInfo()); 11 System.out.println(s3.getInfo()); 12 } 13 } 14 15 class Student 16 { 17 private int number; 18 private String name; 19 private int age; 20 21 Student(int number,String name,int age) 22 { 23 this.setNumber(number); 24 this.setName(name); 25 this.setAge(age); 26 } 27 28 public void setNumber(int number) 29 { 30 if(number>=1) 31 this.number=number; 32 } 33 public int getNumber() 34 { 35 return number; 36 } 37 public void setName(String name) 38 { 39 this.name=name; 40 } 41 public String getName() 42 { 43 return name; 44 } 45 public void setAge(int age) 46 { 47 if(age>=0&&age<135) 48 this.age=age; 49 } 50 public int getAge() 51 { 52 return age; 53 } 54 55 public String getInfo() 56 { 57 return "学号:"+this.getNumber()+",姓名:"+this.getName()+",年龄:"+this.getAge(); 58 } 59 }
时间: 2024-11-02 23:21:55