Matlab - 在类中没有参数的函数 [英] Matlab - Function taking no arguments within a class

查看:219
本文介绍了Matlab - 在类中没有参数的函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于我似乎无法编辑我的旧问题( Matlab - 函数不接受参数但不静态),这里它是再次:

As I do not seem to be able to edit my old question (Matlab - Function taking no arguments but not static), here it is again:

我试图实现以下:

classdef asset
    properties
        name
        values
    end    

    methods

        function AS = asset(name, values)
            AS.name = name;
            AS.values = values;
        end

        function out = somefunction1
            ret = somefunction2(asset.values);
            out = mean(ret);
            return
        end

        function rets = somefunction2(vals)
            n = length(vals);
            rets = zeros(1,n-1);
            for i=1:(n-1)
                rets(i) = vals(i)/vals(i+1);
            end
            return
        end
    end
end


$ b b

但我得到的错误,somefunction1应该是静态的。但是如果它是静态的,那么它不能再访问属性了。我将如何解决这个问题?

But I am getting the error that somefunction1 should be static. But if it's static then it can't access the properties anymore. How would I resolve this issue?

基本上我想要这样写:

AS = asset('testname',[1 2 3 4 5]);
output = AS.somefunction1();

而不是写

AS = asset('testname',[1 2 3 4 5]);
output = AS.somefunction1(AS);


推荐答案

要访问方法中对象的属性,您需要将该对象作为参数传递给方法。如果你不需要一个特定的对象来执行一个函数,那么使它静态(属于类,但不对特定的对象进行操作)。

For accessing the properties of an object in a method, you need to pass that object as argument to the method. If you don't need a specific object in order to perform a function, then make it static (belongs to the class, but does not operate on a specific object).

所以,比较原来的代码:

So, compare the original code:

methods

    % ...

    function out = somefunction1
        ret = somefunction2(asset.values);
        out = mean(ret);
        return
    end;

    function rets = somefunction2(vals)
        n = length(vals);
        rets = zeros(1,n-1);
        for i=1:(n-1)
            rets(i) = vals(i)/vals(i+1);
        end
        return
    end
end

正确的代码:

methods

    % ...

    % this function needs an object to get the data from,
    % so it's not static, and has the object as parameter.

    function out = somefunction1(obj)
        ret = asset.somefunction2(obj.values);
        out = mean(ret);
    end;
end;

methods(Static)
    % this function doesn't depend on a specific object,
    % so it's static.

    function rets = somefunction2(vals)
        n = length(vals);
        rets = zeros(1,n-1);
        for i=1:(n-1)
            rets(i) = vals(i)/vals(i+1);
        end;
    end;
end;

要调用该方法,您必须写入(请测试):

To call the method, you'd write indeed (please test):

AS = asset('testname',[1 2 3 4 5]);
output = AS.somefunction1();

因为在MATLAB中,这是99.99%的情况等效于:

because, in MATLAB, this is 99.99% of cases equivalent to:

AS = asset('testname',[1 2 3 4 5]);
output = somefunction1(AS);

当您覆写 subsref 为类,或者当传递给方法的对象不是参数列表中的第一个(但是这些情况下,你现在不应该关注,直到你澄清MATLAB类语义)。

The differences appear when you're overriding subsref for the class, or when the object passed to the method is not the first in the argument list (but these are cases that you should not concern with for now, until you clarify the MATLAB class semantics).

这篇关于Matlab - 在类中没有参数的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆