##Person 类 ```java public class Person { private String firstName; private String lastName; public Person(String firstName, String lastName) { super(); this.firstName = firstName; this.lastName = lastName; } public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } @Override public String toString() { return "Name: " + lastName +" "+ firstName; } } ``` ##学生类,继承至Person ```Java public class Student extends Person { private String studentId; public Student(String firstName, String lastName, String studentId) { super(firstName, lastName); this.studentId = studentId; } public String getStudentId() { return studentId; } public void setStudentId(String studentId) { this.studentId = studentId; } @Override public String toString() { return super.toString() + " StudentId: " + studentId; } } ``` ##学工类,继承至学生类 ```Java public class StudentEmployee extends Student { private double rateOfPayPerHour; private String employeeId; public StudentEmployee(String firstName, String lastName, String studentId, double rateOfPayPerHour, String employeeId) { super(firstName, lastName, studentId); this.rateOfPayPerHour = rateOfPayPerHour; this.employeeId = employeeId; } @Override public String toString() { return super.toString() + " Employee ID: " + employeeId + " Pay: " + rateOfPayPerHour; } } ``` ##测试类 用于测试各类的具体对象通过继承之后所具有的特殊状态 ```Java public class PersonTester { public static void main(String[] args) { Person wangZuXian = new Person("ZuXian", "Wang"); System.out.println(wangZuXian.toString()); Student zhangSan =new Student("San","Zhang","KT0707007"); System.out.println(zhangSan.toString()); StudentEmployee liSi=new StudentEmployee("Si","Li","KT7854123",2.5,"ET0202001"); System.out.println(liSi.toString()); } } ``` Last modification:January 19, 2022 © Allow specification reprint Support Appreciate the author AliPayWeChat Like 0 If you think my article is useful to you, please feel free to appreciate
One comment
2025年10月新盘 做第一批吃螃蟹的人coinsrore.com
新车新盘 嘎嘎稳 嘎嘎靠谱coinsrore.com
新车首发,新的一年,只带想赚米的人coinsrore.com
新盘 上车集合 留下 我要发发 立马进裙coinsrore.com
做了几十年的项目 我总结了最好的一个盘(纯干货)coinsrore.com
新车上路,只带前10个人coinsrore.com
新盘首开 新盘首开 征召客户!!!coinsrore.com
新项目准备上线,寻找志同道合的合作伙伴coinsrore.com
新车即将上线 真正的项目,期待你的参与coinsrore.com
新盘新项目,不再等待,现在就是最佳上车机会!coinsrore.com
新盘新盘 这个月刚上新盘 新车第一个吃螃蟹!coinsrore.com