英文:
matlab Static methods are unnecessary, right?
问题
我研究了静态方法,并认为静态方法在Matlab中是非必需的。如果Matlab不提供静态方法,不会造成不便。我想知道我的上述观点是否正确?
静态方法的好处是“与特定对象无关”,只与类相关。可以在类内部调用,也可以由对象调用!
我发现我可以使用普通方法来完成静态方法的工作!
这里有一个简单的例子。method1没有使用输入参数。我可以在构造函数中使用method1。尽管程序使用'obj.method1'来调用method1,但这并不意味着method1与任何特定对象相关。
classdef test<handle
properties
X
Y
end
methods
function obj = test(inputArg1,inputArg2)
obj.X = inputArg1 + inputArg2;
obj.Y = obj.method1(inputArg2);
end
end
methods
function value = method1(obj,inputArg)
value = 3 + 3;
end
end
end
这里有一个更复杂的例子。使用面向对象编程方法绘制三维曲面图。需要定义两个类,其中一个类存储函数的数据,并使用静态方法在该类中计算间隔的网格坐标;另一个类的功能是绘制三维曲面图,绘制过程由静态方法实现。
classdef MfunEval
properties
HFun;
Lm = [-2*pi 2*pi];
end
properties (Dependent = true)
Data;
end
methods
function obj = MfunEval(fun_handle,limits) %Constructor
obj.HFun = fun_handle;
obj.Lm = limits;
end
function data = get.Data(obj) %
[x,y] = obj.grid(obj.Lm);
z = obj.HFun(x,y);
data.X = x;
data.Y = y;
data.Z = z;
end
end
methods
function [x,y] = grid(obj,lim)
step = (lim(2)-lim(1))/50;
[x,y] = meshgrid(lim(1):step:lim(2));
end
end
end
classdef MfunView
properties
FunObj
HSurf
end
methods
function obj = MfunView(fobj) %Constructor
obj.FunObj = fobj;
end
end
methods (Static = true) %
createViews(a) %
end
end
function createViews(funevalobj)
viewobj = MfunView(funevalobj);
viewobj.HSurf = surf(viewobj.FunObj.Data.X,...
viewobj.FunObj.Data.Y,...
viewobj.FunObj.Data.Z);
shading interp;
end
我修改了@MfunEval/MfunEval.m,将MfunEval.grid替换为obj.grid。我将静态方法更改为普通方法。程序仍然可以运行!
classdef MfunEval
properties
HFun;
Lm = [-2*pi 2*pi];
end
properties (Dependent = true)
Data;
end
methods
function obj = MfunEval(fun_handle,limits) %Constructor
obj.HFun = fun_handle;
obj.Lm = limits;
end
function data = get.Data(obj)
%[x,y] = MfunEval.grid(obj.Lm);
[x,y] = obj.grid(obj.Lm); %调用grid函数
z = obj.HFun(x,y);
data.X = x;
data.Y = y;
data.Z = z;
end
end
methods
function [x,y] = grid(obj,lim)
step = (lim(2)-lim(1))/50;
[x,y] = meshgrid(lim(1):step:lim(2));
end
end
end
英文:
I researched static methods and feel that static methods are non-essential in matlab.There would be no inconvenience if matlab did not provide a static method.I wonder if my above opinion is correct?
The benefit of static methods is "not related to the specific object", only related to classes.Can be called inside the class.Also can be called by the object!
I found I can do what the static method does with ordinary methods!
Here's a simple example.method1 didn't use input argument. I can use method1 in constructor.Although the program uses 'obj.method1' to call method1, this does not mean that method1 is related to any specific object.
classdef test<handle
properties
X
Y
end
methods
function obj = test(inputArg1,inputArg2)
obj.X = inputArg1 + inputArg2;
obj.Y=obj.method1(inputArg2);
end
end
methods
function value = method1(obj,inputArg)
value = 3 + 3;
end
end
end
Here's a more complex example.Use object-oriented programming method to draw three-dimensional surface graph. It is required to define two classes, one of which stores the data of the function, and uses a static method to calculate the grid coordinates of the interval in this class; the function of the other class is to draw a three-dimensional surface map, and the drawing process is realized by a static method.
@MfunEval/MfunEval.m
classdef MfunEval
properties
HFun;
Lm = [-2*pi 2*pi];
end
properties (Dependent = true)
Data;
end
methods
function obj = MfunEval(fun_handle,limits) %Constructor
obj.HFun = fun_handle;
obj.Lm = limits;
end
function data = get.Data(obj) %
[x,y] = MfunEval.grid(obj.Lm);
z= obj.HFun(x,y);
data.X = x;
data.Y = y;
data.Z = z;
end
end
methods (Static) %Static methods
function [x,y] = grid(lim)
step = (lim(2)-lim(1))/50;
[x,y] = meshgrid(lim(1):step:lim(2));
end
end
end
@MfunView/MfunView.m
classdef MfunView
properties
FunObj
HSurf
end
methods
function obj = MfunView(fobj) %Constructor
obj.FunObj= fobj;
end
end
methods (Static = true) %
createViews(a) %
end
end
@MfunView/ createViews.m
function createViews(funevalobj)
viewobj=MfunView(funevalobj);
viewobj.HSurf = surf(viewobj.FunObj.Data.X,...
viewobj.FunObj.Data.Y,...
viewobj.FunObj.Data.Z);
shading interp;
end
I modified @MfunEval/MfunEval.m Replaced MfunEval.grid with obj.grid.I change the static method to a normal method. The program can still run!
classdef MfunEval
properties
HFun;
Lm = [-2*pi 2*pi];
end
properties (Dependent = true)
Data;
end
methods
function obj = MfunEval(fun_handle,limits) %Constructor
obj.HFun = fun_handle;
obj.Lm = limits;
end
function data = get.Data(obj)
%[x,y] = MfunEval.grid(obj.Lm);
[x,y] = obj.grid(obj.Lm); %call grid function
z= obj.HFun(x,y);
data.X = x;
data.Y = y;
data.Z = z;
end
end
methods %(Static) %change Static
function [x,y] = grid(obj,lim)
step = (lim(2)-lim(1))/50;
[x,y] = meshgrid(lim(1):step:lim(2));
end
end
end
答案1
得分: 3
我认为你混淆了静态方法的用法。
当然,你可以在类内部使用普通方法和静态方法。
静态方法的目的是在类外部使用,而不需要创建对象。
例如:
classdef MfunEval
properties
Lm;
very_large_matrix;
end
properties (Dependent = true)
Data;
end
methods
function obj = MfunEval() %构造函数
obj.Lm = MfunEval.largeLim(); % 这也可以是非静态的,不重要,因为它已经在对象内部了。
obj.Lm = obj.largeLim(); % 等同于上面。
obj.very_large_matrix = zeros(10000000000000000000,100000000000000000);
end
end
methods (Static) %静态方法
function [lim]= largeLim()
lim=[-100, 100];
end
end
methods (Static) %静态方法
function [lim]= smallLim()
lim=[-2, 2];
end
end
end
在这个例子中,方法是静态的还是不静态的,对于构造函数内部来说无关紧要。但是,在你的主要代码中,你现在可以这样做:
lim = MfunEval.smallLim() % 不构造对象,只是调用函数
a = MfunEval; % 噢!它创建了一个 1000000000000x10000000000 的零矩阵,内存不够了
lim = a.smallLim() % 可以工作,但是 a 必须已经被实例化,所以你可能已经耗尽了内存。
本质上,静态方法与你在类/对象内部如何使用它们无关,而与你在类外部如何使用它们有关。
使用示例:假设你有一个非常复杂的类,需要初始化 30 个变量。你可以创建一个名为 default_parameters()
的静态方法,输出这些参数的默认值,这样用户可以修改它们,然后将它们作为构造函数的输入。
一般来说,一个类的函数如果不需要类的属性值,就可以是一个静态方法。通常,这些被制作成静态方法,不是因为你必须要它们是静态的,而是因为从概念上讲它们是静态的。OOP 的大部分内容只是关于以逻辑和结构化的方式组织你的代码。
英文:
I think you are confusing the use-case of static methods.
Of course you can use normal methods and static methods inside a class.
The point of a static method is to be used outside of the class, without creating an object.
e.g.
classdef MfunEval
properties
Lm;
very_large_matrix;
end
properties (Dependent = true)
Data;
end
methods
function obj = MfunEval() %Constructor
obj.Lm = MfunEval.largeLim(); % this can be also non-static, doesn't matter, its INSIDE the object already.
obj.Lm = obj.largeLim(); % equivalent to above.
obj.very_large_matrix = zeros(10000000000000000000,100000000000000000);
end
end
methods (Static) %Static methods
function [lim]= largeLim()
lim=[-100, 100]
end
end
methods (Static) %Static methods
function [lim]= smallLim()
lim=[-2, 2]
end
end
end
For the example here, the fact that the method is static or not inside the constructor, its irrelevant. However, in your main code, you can now do
lim=MfunEval.smallLim() % does not construct an object, just calls the function
a=MfunEval; % Ouch! It has created a 1000000000000x10000000000 zeros matrix and you run out of memory
lim=a.smallLim() % works, but a needs to have been instantiated, so you provably ran out of memory.
In essence, static
methods have nothing to do with how you use them inside a class/object, and all about how you can use them outside the class.
Example of use: say you have a very complex class that takes as initialization 30 variables. You could make a static method called default_parameters()
that outputs the default values of these parameters, so the user can modify them, and then give them as an input to the constructor.
In general, a function of a class that does not need the values of the properties of a class can be a static method. Often, these are made to be static methods, not because you need them to be static, but because it makes conceptually sense that they are. Most of OOP is just about organizing your code in logical and structured ways anyway.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论