Matlab混入模式(Mixin)

Mixin是一種類,這種類包含了其餘類要使用的特性方法,但沒必要充當其餘類的父類。Matlab無疑是支持多繼承的。咱們能夠利用 Matlab 的這種特性,實現一種叫作 Mixin 的類。MixIn的目的就是給一個類增長多個功能,這樣,在設計類的時候,咱們優先考慮經過多重繼承來組合多個MixIn的功能,而不是設計多層次的複雜的繼承關係。(見http://www.javashuo.com/article/p-esidbcmx-gv.html)app

Automobile.m測試

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

Car.mspa

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

Bus.m.net

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)code

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

Blue.m (混入類Mixin)blog

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.mip

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

 測試代碼:get

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

bb = BlueBus();
bb.dispThis();​

參考資料:

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

http://www.javashuo.com/article/p-umxgqwqu-gb.html

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

相關文章
相關標籤/搜索