A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

多态究竟有什么好处呢?我们该怎么使用它呢?

相信很多朋友都有疑惑,我来举一个例子来给在多态迷惑的兄弟们来解答一下。
如果有什么不对之处,请多多指教!!!

  1. /*
  2.         思路:
  3.                 1,定义一个人类
  4.                 2,定义一个教师类
  5.                 3,定义一个工人类
  6.                 4,定义一个护士类
  7.                 5,需要打印出教师类、工人类、护士类各3个对象的show()方法、work()方法和eat()方法
  8.                         5.1        不使用多态操作
  9.                         5.2        使用多态操作
  10.         总结:
  11.                 1,当你对比5.1 和 5.2 之后,相信兄弟们都知道多态操作比非多态的创建对象,更方便、更具有维护性。
  12.                 2,如果还有迷惑的,可以这样想想!
  13.                    当有多了一个司机类,清洁工类,公务员类,非多态又要创建一大堆的对象和调用一大堆方法。
  14.                    而多态只要创建好对象,无论多少方法,只要调用printTool(new 类名());就解决问题了。
  15.                   
  16. */

  17. //1,定义一个人类
  18. class Person
  19. {
  20.         public void show()
  21.         {
  22.                 System.out.println("人 show");
  23.         }

  24.         public void work()
  25.         {
  26.                 System.out.println("人 work");
  27.         }

  28.         public void eat()
  29.         {
  30.                 System.out.println("人 eat");
  31.         }
  32. }

  33. //2,定义一个教师类
  34. class Teacher extends Person
  35. {
  36.         public void show()
  37.         {
  38.                 System.out.println("教师 show");
  39.         }

  40.         public void work()
  41.         {
  42.                 System.out.println("教师教书 work");
  43.         }

  44.         public void eat()
  45.         {
  46.                 System.out.println("教师 eat");
  47.         }
  48. }

  49. //3,定义一个工人类
  50. class Worker extends Person
  51. {
  52.         public void show()
  53.         {
  54.                 System.out.println("工人 show");
  55.         }

  56.         public void work()
  57.         {
  58.                 System.out.println("工人修电 work");
  59.         }

  60.         public void eat()
  61.         {
  62.                 System.out.println("工人 eat");
  63.         }
  64. }

  65. //4,定义一个护士类
  66. class Nurse extends Person
  67. {
  68.         public void show()
  69.         {
  70.                 System.out.println("护士 show");
  71.         }

  72.         public void work()
  73.         {
  74.                 System.out.println("护士打针 work");
  75.         }

  76.         public void eat()
  77.         {
  78.                 System.out.println("护士 eat");
  79.         }
  80. }

  81. //6,定义一个工具类专门来调用show()和work()方法。
  82. //         但是他们都是不同类型的,怎么办?唯有使用多态知识解决!
  83. class PersonTool
  84. {
  85.         //工具类方法通常都定义成静态,不必创建对象,所以定义构造函数为private
  86.         private PersonTool(){}

  87.         //定义一个工具类专门来调用show()和work()方法。
  88.         //【重点看这里使用到的多态,领悟了对你以后有很大的帮助!】
  89.         public static void printTool(Person p)
  90.         {
  91.                 //当这里方法越多,就越需要使用到这样的思想!
  92.                 p.show();
  93.                 p.work();
  94.                 p.eat();
  95.         }
  96. }


  97. class DuotaiTest
  98. {
  99.         public static void main(String[] args)
  100.         {
  101.                 //5,需要打印出教师类、工人类、护士类各3个对象的show()方法、work()方法和eat()方法
  102.                 //5.1        不使用多态操作
  103.                 System.out.println("*******************不使用多态操作*******************");
  104.                 Teacher t1 = new Teacher();
  105.                 t1.show();
  106.                 t1.work();
  107.                 t1.eat();
  108.                 System.out.println();

  109.                 Teacher t2 = new Teacher();
  110.                 t2.show();
  111.                 t2.work();
  112.                 t2.eat();
  113.                 System.out.println();

  114.                 Teacher t3 = new Teacher();
  115.                 t3.show();
  116.                 t3.work();
  117.                 t3.eat();
  118.                 System.out.println("----------------------------------------------------");
  119.                 Worker w1 = new Worker();
  120.                 w1.show();
  121.                 w1.work();
  122.                 w1.eat();
  123.                 System.out.println();

  124.                 Worker w2 = new Worker();
  125.                 w2.show();
  126.                 w2.work();
  127.                 w2.eat();
  128.                 System.out.println();

  129.                 Worker w3 = new Worker();
  130.                 w3.show();
  131.                 w3.work();
  132.                 w3.eat();
  133.                 System.out.println("----------------------------------------------------");
  134.                 Nurse n1 = new Nurse();
  135.                 n1.show();
  136.                 n1.work();
  137.                 n1.eat();
  138.                 System.out.println();

  139.                 Nurse n2 = new Nurse();
  140.                 n2.show();
  141.                 n2.work();
  142.                 n2.eat();
  143.                 System.out.println();

  144.                 Nurse n3 = new Nurse();
  145.                 n3.show();
  146.                 n3.work();
  147.                 n3.eat();
  148.                 System.out.println("----------------------------------------------------");

  149.                 //5.2        使用多态操作
  150.                 System.out.println("*******************使用多态操作*******************");
  151.                 Teacher t4 = new Teacher();
  152.                 Teacher t5 = new Teacher();
  153.                 Teacher t6 = new Teacher();

  154.                 PersonTool.printTool(t4);
  155.                 System.out.println();

  156.                 PersonTool.printTool(t5);
  157.                 System.out.println();

  158.                 PersonTool.printTool(t6);
  159.                 System.out.println("----------------------------------------------------");
  160.                 Worker w4 = new Worker();
  161.                 Worker w5 = new Worker();
  162.                 Worker w6 = new Worker();

  163.                 PersonTool.printTool(w4);
  164.                 System.out.println();

  165.                 PersonTool.printTool(w5);
  166.                 System.out.println();

  167.                 PersonTool.printTool(w6);
  168.                 System.out.println("----------------------------------------------------");
  169.                 Nurse n4 = new Nurse();
  170.                 Nurse n5 = new Nurse();
  171.                 Nurse n6 = new Nurse();

  172.                 PersonTool.printTool(n4);
  173.                 System.out.println();

  174.                 PersonTool.printTool(n5);
  175.                 System.out.println();

  176.                 PersonTool.printTool(n6);
  177.                 System.out.println("************************完************************");
  178.         }
  179. }
复制代码

2 个回复

倒序浏览
不错 很详细
回复 使用道具 举报
多态很牛的,后面迭代器,比较器都用到了
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马