Static methods are unnecessary, right?
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
endI 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 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 static methods, classes, methods MATLAB Answers — New Questions