简体   繁体   English

在不使用Matlab hgsetget抽象类的情况下定义set / get接口

[英]Defining a set/get interface without using matlab hgsetget abstract class

I generally find the set/get interface defined in hgsetget very useful for setting and getting multiple parameters at the same time. 我通常发现hgsetget中定义的set / get接口对于同时设置和获取多个参数非常有用。 I recently found it was especially suited for object construction. 我最近发现它特别适合对象构造。 Example: 例:

classdef testclass < hgsetget

    properties
        A
        B
        C
        D
    end

    methods
        function obj = testclass(varargin)
            if ~isempty(varargin)
               set(obj,varargin{:})
            end
        end
    end
end

Usage: 用法:

>> a = testclass('A',1,'B',2)

a = 

  testclass handle

  Properties:
    A: 1
    B: 2
    C: []
    D: []

Despite the slowness of this interface I'm really happy of the flexibility it provides. 尽管该界面很慢,但我对它提供的灵活性感到非常满意。

What is more annoying for my application is that I obtain a handle class (by inheritance from hgsetget). 对于我的应用程序来说,更烦人的是,我获得了一个句柄类(通过从hgsetget继承)。

To circumvent this my first guess was to construct an abstract class with my set/get definition inside. 为了避免这种情况,我的第一个猜测是用我的set / get定义构造一个抽象类。 Very simply written it gives: 写得很简单,它给出:

classdef (Abstract) myAbstractClass

    methods

        function obj = set(obj,varargin)
            for i = 1:2:length(varargin)
                obj.(varargin{i}) = varargin{i+1};
            end
        end

        function val = get(obj,varargin)
            val = cell(length(varargin),1);
            for i = 1:length(varargin)
                val{i} = obj.(varargin{i});
            end
        end

    end  

end

and then set it as superclass for my test class 然后将其设置为我的测试类的超类

classdef testclass < myAbstractClass

    properties
        A
        B
        C
        D
    end

    methods
        function obj = testclass(varargin)
            if ~isempty(varargin)
                set(obj,varargin{:})
            end
        end
    end
end

However I must misunderstand something in the construction mechanism because here is what happens: 但是,我必须误解构建机制中的某些内容,因为会发生以下情况:

>> a = testclass('A',1,'B',2)

ans = 

  testclass

  Properties:
    A: 1
    B: 2
    C: []
    D: []

  Methods, Superclasses


a = 

  testclass

  Properties:
    A: []
    B: []
    C: []
    D: []

  Methods, Superclasses

If somebody knows the reason of this behavior, I'm totally open to her/his explanations 如果有人知道这种行为的原因,我完全可以接受他/他的解释

Thank you in advance 先感谢您

JM JM

Overall your design looks good, but within your constructor method testclass , you need to write 总体来说,您的设计看起来不错,但是在构造函数方法testclass ,您需要编写

obj = set(obj,varargin{:});

Because you are using a value class rather than a handle class, you need to retrieve the output of your set method, otherwise you'll have set the property of something, and then discarded it and returned a completely different (and empty) obj as output. 因为您使用的是值类而不是句柄类,所以您需要检索set方法的输出,否则您将设置某物的属性,然后将其丢弃并返回一个完全不同(且为空)的obj作为输出。

Also, you need to add a semi-colon after the call to set ; 另外,您需要在set调用之后添加分号; otherwise it will display the intermediate something. 否则它将显示中间内容。

You may also want to revise your opinion of handle classes; 您可能还想修改您对句柄类的看法。 they're really very natural to use (just my opinion, but more natural in an OO context than value-behaviour). 他们真的很自然地使用(只是我的看法,但是在OO环境中比价值行为更自然)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM