当前位置:   article > 正文

java(继承+封装+多态)编程:定义一个人类(Person),包括属性:姓名、性别、年龄、国籍;包括的方法:吃饭、睡觉,工作_java创建一个名为person的类

java创建一个名为person的类
(继承+封装)编程:定义一个人类(Person),包括属性:姓名、性别、年龄、国籍;
                                    包括的方法:吃饭、睡觉,工作
(1)根据人类,定义一个学生子类,增加属性:学校、学号;重写工作方法(实现内容为学习)
(2)根据人类,定义一个工人类,增加属性:单位、工龄;重写工作方法
(3)根据学生类,定义一个学生干部类(StudentLeader),增加属性:职务;增加方法:开会
(4)定义一个测试类,分别创建上述3类具体人物的对象并将信息打印在控制台上.
  1. public class Person {
  2. private String name;
  3. private String sex;
  4. private int age;
  5. private String nationality;
  6. public Person() {
  7. }
  8. public Person(String name, String sex, int age, String nationality) {
  9. this.name = name;
  10. this.sex = sex;
  11. this.age = age;
  12. this.nationality = nationality;
  13. }
  14. public String getName() {
  15. return name;
  16. }
  17. public void setName(String name) {
  18. this.name = name;
  19. }
  20. public String getSex() {
  21. return sex;
  22. }
  23. public void setSex(String sex) {
  24. this.sex = sex;
  25. }
  26. public int getAge() {
  27. return age;
  28. }
  29. public void setAge(int age) {
  30. this.age = age;
  31. }
  32. public String getNationality() {
  33. return nationality;
  34. }
  35. public void setNationality(String nationality) {
  36. this.nationality = nationality;
  37. }
  38. public void eat(){
  39. System.out.println("吃饭");
  40. }
  41. public void sleep(){
  42. System.out.println("睡觉");
  43. }
  44. public void jop(){
  45. System.out.println("工作");
  46. }
  47. @Override
  48. public String toString() {
  49. return "Person{" +
  50. "name='" + name + '\'' +
  51. ", sex='" + sex + '\'' +
  52. ", age=" + age +
  53. ", nationality='" + nationality + '\'' +
  54. '}';
  55. }
  56. }
  57. class StuentPerson extends Person{
  58. private String school;
  59. private int id;
  60. public StuentPerson() {
  61. }
  62. public StuentPerson(String school, int id) {
  63. this.school = school;
  64. this.id = id;
  65. }
  66. public StuentPerson(String name, String sex, int age, String nationality, String school, int id) {
  67. super(name, sex, age, nationality);
  68. this.school = school;
  69. this.id = id;
  70. }
  71. public String getSchool() {
  72. return school;
  73. }
  74. public void setSchool(String school) {
  75. this.school = school;
  76. }
  77. public int getId() {
  78. return id;
  79. }
  80. public void setId(int id) {
  81. this.id = id;
  82. }
  83. @Override
  84. public void jop() {
  85. System.out.println("学生学习");
  86. }
  87. }
  88. class WorkerPenson extends Person{
  89. private String workplace;
  90. private int seniority;
  91. public WorkerPenson() {
  92. }
  93. public WorkerPenson(String workplace, int seniority) {
  94. this.workplace = workplace;//单位
  95. this.seniority = seniority;//工龄
  96. }
  97. public WorkerPenson(String name, String sex, int age, String nationality, String workplace, int seniority) {
  98. super(name, sex, age, nationality);
  99. this.workplace = workplace;
  100. this.seniority = seniority;
  101. }
  102. public String getWorkplace() {
  103. return workplace;
  104. }
  105. public void setWorkplace(String workplace) {
  106. this.workplace = workplace;
  107. }
  108. public int getSeniority() {
  109. return seniority;
  110. }
  111. public void setSeniority(int seniority) {
  112. this.seniority = seniority;
  113. }
  114. @Override
  115. public void jop() {
  116. System.out.println("工人工作");
  117. }
  118. }
  119. class StudentLeader extends StuentPerson{
  120. private String post;//职务
  121. public StudentLeader() {
  122. }
  123. public StudentLeader(String post) {
  124. this.post = post;
  125. }
  126. public StudentLeader(String school, int id, String post) {
  127. super(school, id);
  128. this.post = post;
  129. }
  130. public StudentLeader(String name, String sex, int age, String nationality, String school, int id, String post) {
  131. super(name, sex, age, nationality, school, id);
  132. this.post = post;
  133. }
  134. public String getPost() {
  135. return post;
  136. }
  137. public void setPost(String post) {
  138. this.post = post;
  139. }
  140. public void meeting(){
  141. System.out.println("学生干部开会");
  142. }
  143. }
  144. class PensonTest{
  145. public static void main(String[] args) {
  146. StuentPerson stuentPerson = new StuentPerson("张三","男",21,"中国","四川工业",101);
  147. stuentPerson.jop();
  148. System.out.println(stuentPerson.getName()+stuentPerson.getSex()+stuentPerson.getAge()+stuentPerson.getNationality()+stuentPerson.getSchool()+stuentPerson.getId());
  149. WorkerPenson workerPenson = new WorkerPenson("李四","女",32,"中国","中信银行",3);
  150. workerPenson.jop();
  151. System.out.println(workerPenson.getName()+workerPenson.getSex()+workerPenson.getAge()+workerPenson.getNationality()+workerPenson.getWorkplace()+workerPenson.getSeniority());
  152. StudentLeader studentLeader = new StudentLeader("学生会主席");
  153. studentLeader.jop();
  154. studentLeader.meeting();
  155. System.out.println(stuentPerson.getName()+stuentPerson.getSex()+stuentPerson.getAge()+stuentPerson.getNationality()+stuentPerson.getSchool()+stuentPerson.getId()+studentLeader.getPost());
  156. stuentPerson.eat();
  157. stuentPerson.sleep();
  158. workerPenson.eat();
  159. workerPenson.sleep();
  160. studentLeader.eat();
  161. studentLeader.sleep();
  162. }
  163. }
(多态)在上一个题目的基础上,定义一个Person类型的数组,存储多个不同类型的子类型对象,
(1)统计并打印输出数组中所有学生干部的个数
(2)打印输出所有学生的信息
  1. public abstract class Person {
  2. private String name;
  3. private String sex;
  4. private int age;
  5. private String nationality;
  6. public Person() {
  7. }
  8. public Person(String name, String sex, int age, String nationality) {
  9. this.name = name;
  10. this.sex = sex;
  11. this.age = age;
  12. this.nationality = nationality;
  13. }
  14. public String getName() {
  15. return name;
  16. }
  17. public void setName(String name) {
  18. this.name = name;
  19. }
  20. public String getSex() {
  21. return sex;
  22. }
  23. public void setSex(String sex) {
  24. this.sex = sex;
  25. }
  26. public int getAge() {
  27. return age;
  28. }
  29. public void setAge(int age) {
  30. this.age = age;
  31. }
  32. public String getNationality() {
  33. return nationality;
  34. }
  35. public void setNationality(String nationality) {
  36. this.nationality = nationality;
  37. }
  38. public void eat() {
  39. System.out.println("吃饭");
  40. }
  41. public void sleep() {
  42. System.out.println("睡觉");
  43. }
  44. public abstract void jop();
  45. @Override
  46. public String toString() {
  47. return "姓名='" + name + '\'' +
  48. ", 性别='" + sex + '\'' +
  49. ", 年龄=" + age +
  50. ", 国籍='" + nationality;
  51. }
  52. }
  53. class StudentPerson extends Person {
  54. private String school;
  55. private int id;
  56. public StudentPerson() {
  57. }
  58. public StudentPerson(String school, int id) {
  59. this.school = school;
  60. this.id = id;
  61. }
  62. public StudentPerson(String name, String sex, int age, String nationality, String school, int id) {
  63. super(name, sex, age, nationality);
  64. this.school = school;
  65. this.id = id;
  66. }
  67. public String getSchool() {
  68. return school;
  69. }
  70. public void setSchool(String school) {
  71. this.school = school;
  72. }
  73. public int getId() {
  74. return id;
  75. }
  76. public void setId(int id) {
  77. this.id = id;
  78. }
  79. @Override
  80. public void jop() {
  81. System.out.println("学生学习");
  82. }
  83. @Override
  84. public String toString() {
  85. return super.toString()+" ,学校:"+school+" ,学号:"+id;
  86. }
  87. }
  88. class WorkerPenson extends Person {
  89. private String workplace;
  90. private int seniority;
  91. public WorkerPenson() {
  92. }
  93. public WorkerPenson(String workplace, int seniority) {
  94. this.workplace = workplace;//单位
  95. this.seniority = seniority;//工龄
  96. }
  97. public WorkerPenson(String name, String sex, int age, String nationality, String workplace, int seniority) {
  98. super(name, sex, age, nationality);
  99. this.workplace = workplace;
  100. this.seniority = seniority;
  101. }
  102. public String getWorkplace() {
  103. return workplace;
  104. }
  105. public void setWorkplace(String workplace) {
  106. this.workplace = workplace;
  107. }
  108. public int getSeniority() {
  109. return seniority;
  110. }
  111. public void setSeniority(int seniority) {
  112. this.seniority = seniority;
  113. }
  114. @Override
  115. public void jop() {
  116. System.out.println("工人工作");
  117. }
  118. @Override
  119. public String toString() {
  120. return super.toString()+" ,单位:"+workplace+" ,工龄:"+seniority;
  121. }
  122. }
  123. class StudentLeader extends StudentPerson {
  124. private String post;//职务
  125. public StudentLeader() {
  126. }
  127. public StudentLeader(String post) {
  128. this.post = post;
  129. }
  130. public StudentLeader(String school, int id, String post) {
  131. super(school, id);
  132. this.post = post;
  133. }
  134. public StudentLeader(String name, String sex, int age, String nationality, String school, int id, String post) {
  135. super(name, sex, age, nationality, school, id);
  136. this.post = post;
  137. }
  138. public String getPost() {
  139. return post;
  140. }
  141. public void setPost(String post) {
  142. this.post = post;
  143. }
  144. public void meeting() {
  145. System.out.println("学生干部开会");
  146. }
  147. @Override
  148. public String toString() {
  149. return super.toString()+"职务:"+post;
  150. }
  151. }
  152. class PersonTest {
  153. public static void main(String[] args) {
  154. WorkerPenson worker = new WorkerPenson("张三","男",23,"中国","中国石油",2);
  155. Person worker1 = new WorkerPenson("李四","男",30,"中国","中国联通",6);
  156. StudentPerson studentPerson = new StudentPerson("马瑞","女",30,"中国","四川工业",101);
  157. Person studentPerson1 = new StudentPerson("杰克","女",23,"中国","西南财经",201);
  158. StudentLeader studentLeader = new StudentLeader("汤姆","男",34,"中国","四川工业",108,"文体部");
  159. Person studentLeader1 = new StudentLeader("肉丝","男",19,"中国","西南",112,"外联部");
  160. Person studentLeader2 = new StudentLeader("白菜","男",20,"中国","西南",333,"全媒体");
  161. Person[] person = new Person[]{worker,worker1,studentPerson,studentPerson1,studentLeader,studentLeader1,studentLeader2};
  162. int count=0;
  163. for (int i=0;i<person.length;i++){
  164. if ((person[i] instanceof StudentLeader)==true){
  165. count++;
  166. }
  167. if ((person[i] instanceof StudentPerson) ==true){
  168. System.out.println(person[i].toString());
  169. }
  170. }
  171. System.out.println("学生干部的个数为:"+count);
  172. }
  173. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/521532
推荐阅读
相关标签
  

闽ICP备14008679号