1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
| package Exception;
import java.util.Scanner;
public class Program { public static void main(String[] args) { Student student=new Student(); String name,dep; int age; Scanner scanner=new Scanner(System.in); System.out.println("请输入学生姓名:"); name=scanner.next(); System.out.println("请输入学生年龄:"); age=scanner.nextInt(); System.out.println("请输入学生院系:"); dep=scanner.next();
student.setNameAgeDep(name,age,dep); student.show(); System.out.println(student); Student student2=new Student(); student2.setNameAgeDep("AAA",20,"Information"); if(student.equals(student2))System.out.println("学生重名"); else System.out.println("学生不重名"); } }
class Person { private String name; private int age;
public String getName() { return name; } public int getAge() { return age; }
public Person() { System.out.println("调用了个人构造方法Person()"); }
public void SetNameAge(String name,int age)throws AgeException { this.name=name; if(age<=0age>=25)throw new AgeException(); this.age=age; }
public void show() { System.out.println("姓名:"+name+",年龄:"+age); } }
class Student extends Person { private String dep;
public void setNameAgeDep(String name,int age,String dep) { try { super.SetNameAge(name,age); } catch (AgeException e) { System.out.println("\nYou type a worng age info.\n"); } this.dep=dep; }
public Student() { System.out.println("也调用了学生构造方法Student()"); }
public void show() { super.show(); System.out.println("院系:" + dep); }
public String toString() { return "学生姓名为"+getName()+",年龄为"+getAge()+",院系为"+dep+"。"; }
public boolean equals(Student student) { if(getName().equals(student.getName()))return true; else return false; } }
|