Matlab相当于调用内部的静态类 [英] Matlab equivalent to calling inside static class

查看:92
本文介绍了Matlab相当于调用内部的静态类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

提供以下代码:

classdef highLowGame
    methods(Static)
        function [wonAmount, noGuesses] = run(gambledAmount)
            noGuesses = 'something';
            wonAmount = highLowGame.getPayout(gambledAmount, noGuesses); % <---
        end
        function wonAmount = getPayout(gambledAmount, noGuesses)
            wonAmount = 'something';
        end
    end
end

一个静态方法的同一个类(在一个静态)方法,而不必写类的名称?类似self.getPayout(...) - 如果类得到500行,我想重命名它。

Is there a way to call a static method of the same class (inside a static) method without having to write the class name? Something like "self.getPayout(...)" - in case the class turns out to get to 500 lines and I want to rename it.

推荐答案

据我所知,no有一个but。通常,只能使用类名指定静态方法。然而,你可以假装你的方式围绕限制,因为MATLAB有feval:

As far as I can tell, "no" with a "but". In general, you can only specify the static method with the class name. However, you can fake your way around the restriction since MATLAB has feval:

classdef testStatic

    methods (Static)
        function p = getPi()  %this is a static method
            p = 3.14;
        end
    end

    methods
        function self = testStatic()

            testStatic.getPi  %these are all equivalent
            feval(sprintf('%s.getPi',class(self)))
            feval(sprintf('%s.getPi',mfilename('class')))
        end
    end
end

这里,class(s​​elf)和mfilename都计算为'testStatic'上面的函数结束评估'testStatic.getPi'。

Here, class(self) and mfilename both evaluate to 'testStatic', so the functions above end up evaluating 'testStatic.getPi'.

或者,您可以编写一个非静态方法self.callStatic;然后总是使用它。在里面,只需调用testStatic.getPi。然后你只需要改变那一行。

Or, alteratively, you can write a non-static method, self.callStatic; then always use that. Inside that, just call testStatic.getPi. Then you'll only need to change that one line.

这篇关于Matlab相当于调用内部的静态类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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