문제

is it possible to call a static method from another static method, both method belonging to the same class? Or is there some special syntax that I am not aware of? Currently I am trying to do the following:

% Circle.m
classdef Circle
    methods (Static)
        function p = getPi
            p = 3.14;
        end

        function a = getArea( r )
            a = r * Circle.getPi * Circle.getPi;
        end
    end
end

And then:

>> Circle.getPi  
ans =  
3.1400  

>> Circle.getArea(123)  
Undefined variable "Circle" or class "Circle.getPi".  

Error in utils.Circle.getArea (line 8)  
            a = r * Circle.getPi * Circle.getPi; 
도움이 되었습니까?

해결책 2

The error message shows that your class is utils.Circle, not Circle. Your class is placed inside a package utils.

다른 팁

See the section Referencing Package Members Within Packages in the documentation page called "Packages Create Namespaces". Basically it says that normal methods from instances of a class do not require the package prefix, but with static methods it is required. Evidently this applies even when calling a static method, from another class method!

All references to packages, functions, and classes in the package must use the package name prefix, unless you import the package. (See Importing Classes.) For example, call a package function with this syntax:

z = mypack.pkfcn(x,y);

Note that definitions do not use the package prefix. For example, the function definition line of the pkfcn.m function would include only the function name:

[snip]

Calling class methods does not require the package name because you have an instance of the class:

obj.myMethod(arg) or  
myMethod(obj,arg)

A static method requires the full class name:

mypack.myClass.stMethod(arg)
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top