经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » Java相关 » 设计模式 » 查看文章
Matlab混入模式(Mixin) - sw-lab
来源:cnblogs  作者:sw-lab  时间:2019/6/19 8:58:36  对本文有异议

Mixin是一种类,这种类包含了其他类要使用的特性方法,但不必充当其他类的父类。Matlab无疑是支持多继承的。我们可以利用 Matlab 的这种特性,实现一种叫做 Mixin 的类。MixIn的目的就是给一个类增加多个功能,这样,在设计类的时候,我们优先考虑通过多重继承来组合多个MixIn的功能,而不是设计多层次的复杂的继承关系。(见https://blog.csdn.net/qq_31156277/article/details/80659537)

Automobile.m

  1. classdef Automobile < handle
  2. methods(Abstract)
  3. dispAutomobile(~);
  4. end
  5. end

Car.m

  1. classdef Car < Automobile
  2. methods
  3. function dispAutomobile(~)
  4. disp("Car");
  5. end
  6. end
  7. end

Bus.m

  1. classdef Bus < Automobile
  2. methods
  3. function dispAutomobile(~)
  4. disp("Bus");
  5. end
  6. end
  7. end

Color.m (混入类Mixin)

  1. classdef Color < handle
  2. methods(Abstract)
  3. dispColor(~);
  4. end
  5. end

Red.m(混入类Mixin)

  1. classdef Red < Color
  2. methods
  3. function dispColor(~)
  4. disp("Red");
  5. end
  6. end
  7. end

Blue.m (混入类Mixin)

  1. classdef Blue < Color
  2. methods
  3. function dispColor(~)
  4. disp("Blue");
  5. end
  6. end
  7. end

RedCar.m

  1. classdef RedCar < Car & Red
  2. methods
  3. function dispThis(obj)
  4. disp("RedCar is:");
  5. obj.dispColor();
  6. obj.dispAutomobile();
  7. end
  8. end
  9. end

BlueBus.m

  1. classdef BlueBus < Bus & Blue
  2. methods
  3. function dispThis(obj)
  4. disp("BlueBus is:");
  5. obj.dispColor();
  6. obj.dispAutomobile();
  7. end
  8. end
  9. end

 测试代码:

  1. rc = RedCar();
  2. rc.dispThis();
  3.  
  4. bb = BlueBus();
  5. bb.dispThis();?

参考资料:

https://blog.csdn.net/cwy0502/article/details/90924330

https://blog.csdn.net/u012814856/article/details/81355935

https://blog.csdn.net/weixin_34006468/article/details/87266145

https://blog.csdn.net/zhongbeida_xue/article/details/88601352

https://blog.csdn.net/u013985879/article/details/82155892

原文链接:http://www.cnblogs.com/usaddew/p/11048431.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号