组合继承代码

  1. import java.util.Scanner;
  2. class Human { //人类
  3. private String name;
  4. private int age;
  5. private String cha;
  6. public String think;
  7. public void getThink(String think) //思考方法
  8. {
  9. this.think=think;
  10. }
  11. public void setName(String name,int age,String cha) //构造方法 构造姓名 年龄 性别
  12. {
  13. this.name = name;
  14. this.age=age;
  15. this.cha=cha;
  16. }
  17. public String getName(String name){ //转换为保护数据
  18. return this.name;
  19. }
  20. public int getAge(int age){
  21. return this.age;
  22. }
  23. }
  24. class Pupil extends Human { //学生类
  25. public String style;
  26. private String number;
  27. public void getstyle(String style) //学习方法
  28. {
  29. this.style=style;
  30. }
  31. public void setstyle(String style) //构造方法
  32. {
  33. this.style = style;
  34. }
  35. public String getnumber() //获取学号
  36. {
  37. return this.number;
  38. }
  39. public void number(String number) //转化为受保护数据
  40. {
  41. this.number = number;
  42. }
  43. }
  44. class Student extends Pupil //大学生类
  45. {
  46. public String style1;
  47. public void setstyle1(String style1) //构造方法
  48. {
  49. this.style1 = style1;
  50. }
  51. public void getstyle1(String style) //学习方法
  52. {
  53. this.style1=style;
  54. }
  55. void printf(String name,int age,String cha,String number,String style) //打印方法
  56. {
  57. System.out.printf("%s %d %s %s %s\n",name,age,cha,number,style);
  58. }
  59. }
  60. class example //主入口类 列举例子
  61. {
  62. String s1="zhang",s2="liubu",s3="kojlih";
  63. int a1=18,a2=19,a3=20;
  64. String c1="man",c2="women",c3="man";
  65. String n1="1804010328",n2="1804010119",n3="1802039087";
  66. String h1="reading",h2="listening",h3="running";
  67. }
  68. public class Main {
  69. public static void main(String[] args) {
  70. Scanner AI=new Scanner(System.in);
  71. Human HH=new Human();
  72. Pupil PP=new Pupil();
  73. Student SS=new Student();
  74. example EE=new example();
  75. SS.printf(EE.s1, EE.a1, EE.c1, EE.n1, EE.h1);
  76. SS.printf(EE.s2, EE.a2, EE.c2, EE.n2, EE.h2);
  77. SS.printf(EE.s3, EE.a3, EE.c3, EE.n3, EE.h3);
  78. }
  79. }
上一篇:TCP/IP


下一篇:Java EE 网上书店之需求分析与概要设计