Matlab混入模式(Mixin) - Go语言中文社区

Matlab混入模式(Mixin)


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

Automobile.m

classdef Automobile < handle
    methods(Abstract)
        dispAutomobile(~);
    end
end

Car.m

classdef Car < Automobile
    methods
        function dispAutomobile(~)
            disp("Car");
        end
    end
end

Bus.m

classdef Bus < Automobile
    methods
        function dispAutomobile(~)
            disp("Bus");
        end
    end
end

Color.m (混入类Mixin)

classdef Color < handle
    methods(Abstract)
        dispColor(~);
    end
end

Red.m(混入类Mixin)

classdef Red < Color
    methods
        function dispColor(~)
            disp("Red");
        end
    end
end

Blue.m (混入类Mixin)

classdef Blue < Color
    methods
        function dispColor(~)
            disp("Blue");
        end
    end
end

RedCar.m

classdef RedCar < Car & Red
    methods
        function dispThis(obj)
            disp("RedCar is:");
            obj.dispColor();
            obj.dispAutomobile();
        end
    end
end

BlueBus.m

classdef BlueBus < Bus & Blue
    methods
        function dispThis(obj)
            disp("BlueBus is:");
            obj.dispColor();
            obj.dispAutomobile();
        end
    end
end

 测试代码:

rc = RedCar();
rc.dispThis();

bb = BlueBus();
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

版权声明:本文来源博客园,感谢博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://www.cnblogs.com/usaddew/p/11048431.html
站方申明:本站部分内容来自社区用户分享,若涉及侵权,请联系站方删除。
  • 发表于 2019-11-16 23:56:41
  • 阅读 ( 1199 )
  • 分类:

0 条评论

请先 登录 后评论

官方社群

GO教程

推荐文章

猜你喜欢