Defined method can not be invoked.
2 次查看(过去 30 天)
显示 更早的评论
I want to extend ss object. But, the methods can not de invoked in the file.
Am I using wrong way?
Here is my object
classdef ssSpring < ss
%SSSPRING Summary of this class goes here
% Detailed explanation goes here
properties (Access = public)
x0;
end
methods
function sys = ssSpring()
sys.A=[0,0,0,1,0,0;0,0,0,0,1,0;0,0,0,0,0,1;-1,1,0,0,0,0;1,-2,1,0,0,0;0,1,-1,0,0,0];
sys.B=[0;0;0;1;0;0];
sys.C=[0,0,1,0,0,0];
sys.D=[0];
sys.x0=[0.323811195852370;0.430986087686128;0.764897800510776;0.996462675076854;0.265018285144099;0.792067001328556];
end
end
methods (Access=public)
function b = printa(a)
b=a;
end
end
end
2 个评论
采纳的回答
Walter Roberson
2023-1-5
The first parameter to a non-static non-constructor class method is always the class object that is being operated on. That would be sys in the context of your code. You then attempt to pass a second parameter with value 1 but the method is only defined to accept one parameter.
sys.printa(1)
is the same as
printa(sys, 1)
2 个评论
Steven Lord
2023-1-5
The first parameter to a non-static non-constructor class method is always the class object that is being operated on.
That's not always correct. At least one of the inputs to a non-Static and non-constructor class method must be an instance of the class, but it does not necessarily need to be the first input. As an example both of the following calls would use the plus method of the sym class even those in the second case the first input is the double value 1.
syms x
which plus(x,1)
which plus(1,x)
If both the inputs are objects, the rules for which class's method gets called are listed here. When InferiorClasses are involved, the first object is not always the one whose method gets called.
But your point about the method needing to be defined to accept two inputs rather than just one is correct and is the root cause of the error @younghwa park received.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Construct and Work with Object Arrays 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!