一:多态数组,对象数组

数组的定义类型父类类型,里面保存的实际元素魏子类类型

1:应用实例

image.png

  1. public class arrtest00 {
  2. public static void main(String[] args) {
  3. //对象数组
  4. Person[] person = new Person[5];
  5. //意味着person以及person的子类都可以放进去
  6. person[0] = new Person("sakura",18);
  7. person[1] = new Teacher("Tom",50,25000);
  8. person[2] = new Student("jack",20,100);
  9. for (int i = 0; i < 3; i++) {
  10. System.out.println(person[i].say());
  11. //如何调用子类的特有方法
  12. if (person[i] instanceof Student){
  13. // Student student = (Student) person[i];//向下转型
  14. ((Student)person[i]).study();//匿名对象的向下转型
  15. }else if (person[i] instanceof Teacher){
  16. ((Teacher)person[i]).teach();
  17. }
  18. }
  19. }
  20. }
  21. package Date0802.Test00;
  22. /**
  23. * 作者:sakura
  24. * 日期:2022年08月03日 11:59
  25. */
  26. public class Person {
  27. private String name;
  28. private int age;
  29. public Person(String name, int age) {
  30. this.name = name;
  31. this.age = age;
  32. }
  33. public String getName() {
  34. return name;
  35. }
  36. public void setName(String name) {
  37. this.name = name;
  38. }
  39. public int getAge() {
  40. return age;
  41. }
  42. public void setAge(int age) {
  43. this.age = age;
  44. }
  45. public String say (){
  46. return "name:"+this.name + "\t"+"age:"+ this.age;
  47. }
  48. }
  49. package Date0802.Test00;
  50. /**
  51. * 作者:sakura
  52. * 日期:2022年08月03日 11:59
  53. */
  54. public class Student extends Person{
  55. private int Score;
  56. public Student(String name, int age, int score) {
  57. super(name, age);
  58. Score = score;
  59. }
  60. public int getScore() {
  61. return Score;
  62. }
  63. public void setScore(int score) {
  64. Score = score;
  65. }
  66. @Override
  67. public String say() {
  68. return "(学生)"+super.say() +"\t"+ "score:"+"\t"+this.Score;
  69. }
  70. public void study(){
  71. System.out.println("学生:"+getName()+"正在学习");
  72. }
  73. }
  74. package Date0802.Test00;
  75. /**
  76. * 作者:sakura
  77. * 日期:2022年08月03日 12:00
  78. */
  79. public class Teacher extends Person{
  80. private int salary;
  81. public Teacher(String name, int age, int salary) {
  82. super(name, age);
  83. this.salary = salary;
  84. }
  85. public int getSalary() {
  86. return salary;
  87. }
  88. public void setSalary(int salary) {
  89. this.salary = salary;
  90. }
  91. @Override
  92. public String say() {
  93. return "(老师)"+super.say() +"\t"+ "salary:" + this.salary;
  94. }
  95. public void teach(){
  96. System.out.println("老师:"+ getName()+ "正在授课");
  97. }
  98. }

二:多态参数

1:应用举例

image.png

  1. package Date0802.Test01;
  2. /**
  3. * 作者:sakura
  4. * 日期:2022年08月03日 15:02
  5. */
  6. public class Test01 {
  7. public static void main(String[] args) {
  8. staff tom = new staff("tom", 5000);
  9. Managerment sakura = new Managerment("sakura", 10000, 9999);
  10. Test01 T = new Test01();
  11. T.showEmpAnn(sakura);
  12. T.showEmpAnn(tom);
  13. T.testWork(sakura);
  14. T.testWork(tom);
  15. }
  16. public void showEmpAnn(Employee E){
  17. E.getAnnual();
  18. }
  19. public void testWork(Employee E){
  20. if (E instanceof staff){
  21. ((staff)E).work();
  22. }else if(E instanceof Managerment){
  23. ((Managerment)E).manager();
  24. }
  25. }
  26. }
  1. package Date0802.Test01;
  2. /**
  3. * 作者:sakura
  4. * 日期:2022年08月03日 14:49
  5. */
  6. public class Employee {
  7. private String name;
  8. private int salary;
  9. public Employee(String name, int salary) {
  10. this.name = name;
  11. this.salary = salary;
  12. }
  13. public String getName() {
  14. return name;
  15. }
  16. public void setName(String name) {
  17. this.name = name;
  18. }
  19. public int getSalary() {
  20. return salary;
  21. }
  22. public void setSalary(int salary) {
  23. this.salary = salary;
  24. }
  25. public void getAnnual(){
  26. System.out.println(12*getSalary());
  27. }
  28. }
  1. package Date0802.Test01;
  2. /**
  3. * 作者:sakura
  4. * 日期:2022年08月03日 15:00
  5. */
  6. public class Managerment extends Employee{
  7. private int bonus;
  8. public Managerment(String name, int salary, int bonus) {
  9. super(name, salary);
  10. this.bonus = bonus;
  11. }
  12. public int getBonus() {
  13. return bonus;
  14. }
  15. public void setBonus(int bonus) {
  16. this.bonus = bonus;
  17. }
  18. @Override
  19. public void getAnnual() {
  20. System.out.print("经理的"+getName()+"年工资:");
  21. super.getAnnual();
  22. }
  23. public void manager(){
  24. System.out.println("经理管理员工");
  25. }
  26. }
  1. package Date0802.Test01;
  2. /**
  3. * 作者:sakura
  4. * 日期:2022年08月03日 14:55
  5. */
  6. public class staff extends Employee{
  7. public staff(String name, int salary) {
  8. super(name, salary);
  9. }
  10. public void work(){
  11. System.out.println("员工工作");
  12. }
  13. @Override
  14. public void getAnnual() {
  15. System.out.print("员工"+getName()+"的年工资:");
  16. super.getAnnual();
  17. }
  18. }