Matlab equivalent to calling inside static class

前端 未结 2 598
礼貌的吻别
礼貌的吻别 2020-12-18 05:55

Confer the following code:

classdef highLowGame
    methods(Static)
        function [wonAmount, noGuesses] = run(gambledAmount)
            noGuesses = \'so         


        
2条回答
  •  伪装坚强ぢ
    2020-12-18 06:46

    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
    

    Here, class(self) and mfilename both evaluate to 'testStatic', so the functions above end up evaluating '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.

提交回复
热议问题