class method overloading question

1 次查看(过去 30 天)
JFz
JFz 2016-2-12
评论: Guillaume 2016-2-12
I have a handle class called DBHandle. I have a method runquery in it. I have a subclass SqlDBhandle in which I am not defining the method runquery explicitly.
Will an SqlDBHandle pick up the runquery from the DBHandle? I keep getting error: 'Too many input arguments'. Thanks.
jennifer

回答(1 个)

Guillaume
Guillaume 2016-2-12
编辑:Guillaume 2016-2-12
The subclass automatically inherits all public and protected (but not private) methods of the baseclass. So, yes, as long as runquery is public or protected, SqlDBHandle will use the runquery method defined in DBHandle.
'Too many input arguments' does not sound like an error that has to do with inheritance. How are you calling runquery and how is it defined?
  2 个评论
JFz
JFz 2016-2-12
编辑:Guillaume 2016-2-12
Thank you so much. I define runquery in DBHandle as:
function obj = runquery(inSql)
%nothing but obj
obj=0;
end
And I call it this way:
lkTbl = sqlDbHand.runquery(sSql);
Guillaume
Guillaume 2016-2-12
Well, yes your call to runquery does not match the method signature. Note that in matlab this:
lkTbl = sqlDbHand.runquery(sSql);
is actually translated by matlab interpreted into
lkTbl = runquery(sqlDbHand, sSql);
You can see that you are actually passing two arguments to runquery. The object itself, and the sSql argument. Therefore, the signature or runquery should be:
function result = runquery(this, inSql)
result = ...
end
Note that I've changed the name of the return value from obj to something else. I'm not sure what you meant by obj, but if you meant the DBHandle class object, it's not an output, it's an input which I've called this (as that's the standard name in C++ and C#)

请先登录,再进行评论。

标签

产品

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by