gpt4 book ai didi

matlab - 从另一个静态方法中调用 matlab 静态方法

转载 作者:行者123 更新时间:2023-12-04 12:52:33 24 4
gpt4 key购买 nike

是否可以从另一个静态方法调用静态方法,这两个方法属于同一个类?还是有一些我不知道的特殊语法?目前我正在尝试执行以下操作:

% 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

然后:

>> 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;

最佳答案

参见 Referencing Package Members Within Packages 部分在名为 "Packages Create Namespaces" 的文档页面中.基本上它说来自类实例的普通方法不需要包前缀,但是对于静态方法它是必需的。显然,这甚至适用于从另一个类方法调用静态方法!

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)

关于matlab - 从另一个静态方法中调用 matlab 静态方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22967511/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com