3

我有 2 节课:

classdef Car
    properties 
        make;
    end
    methods
        function obj=Car(MyMake)
            obj.make=MyMake;
        end
    end
end

classdef Engine
    properties
        power;
    end
    methods
        function obj=Engine(bhp)
            obj.power=bhp;
        end
    end
end

我应该如何将 Engine 类嵌套在 Car 类中?

我的想法是按照以下方式做一些事情Honda.Engine.Valve.getDiameter()

在 Python 中,我需要做的就是缩进,我不知道在这里做什么。

4

1 回答 1

1

我不相信你可以在 Matlab 中嵌套类。但是,您仍然可以(或多或少地)使用常规类并将“嵌套类”分配为“包含类”的属性来实现您想要的结果。

例如:

classdef Nested
    methods
        function this=Nested()
            % Yep.
        end
    end
end

classdef Box
    properties
        nested
    end
    methods
        function this=Box()
            this.nested = Nested();
        end
    end
end

这将不允许“嵌套”类访问“包含”类的属性,但它可以提供与您描述的相同的嵌套访问。

有关详细信息,请参阅http://www.mathworks.com/help/matlab/matlab_oop/saving-class-files.html。例如,此页面指出:

在 MATLAB® 路径上的文件夹中创建一个独立的类定义文件。文件的名称必须与类(和构造函数)名称匹配,并且必须具有 .m 扩展名。在这个文件中完全定义类。

因此,嵌套类将违反此语法,因为它们不包含在 Matlab 路径上自己的文件中。

我以前没有使用过 Matlab 包,但是您可以使用包来确定您的特定组织和功能。见http://www.mathworks.com/help/matlab/matlab_oop/scoping-classes-with-packages.html

祝你好运!

于 2013-09-01T00:02:43.823 回答