Call superclass method without knowing name of superclass?
古いコメントを表示
I would like to call a method of a superclass from a subclass after performing some additional operations within the subclass method. However, to make the code general (say I change the superclass name or add an intermediate class later between the super- and subclass), I would like to be able to call the method without explicitly naming the superclass, which would prevent me from using the standard command:
function_name@superclass_of_interest(arg1,arg2,etc)
I have thought of two ways of doing this, neither of which I am enamored of.
First, use an eval statement:
eval(['function_name@' superclass_name '(arg1,arg2,etc)'])
Second, create a "glue class", which subclasses the actual class of interest and contains nothing more than a constructor:
classdef glue_class < superclass_of_interest
methods
function obj = glue_class(varargin)
obj = obj@superclass_of_interest(varargin{:});
end
end
end
Now if I subclass the glue_class instead of the superclass_of_interest, I only need to change the class name within a the (simple) glue_demo file instead of in every function of my actual class from which I would like to call a superclass method.
Is there a more elegant solution? My naive attempts at creating function handles to the superclass method failed, as did trying to use an feval statement.
EDIT: per Daniel's answer, the "glue class" (which MATLAB calls an alias class) described in the second proposal above appears to be MATLAB's recommended solution.
5 件のコメント
Andrew Newell
2011 年 7 月 20 日
Just to be clear here - have you overloaded this method in your subclass?
Jared
2011 年 7 月 20 日
Andrew Newell
2011 年 7 月 20 日
Yes.
Daniel Shub
2011 年 7 月 21 日
You are right that having to specify the name of the superclass method makes maintaing code, especially in the development stage, a real pain. I think the problem is that a class can be a direct subclass of two classes and making an automated system to handle this, generally rare case, is probably painful.
Jared
2011 年 7 月 21 日
採用された回答
その他の回答 (2 件)
Andrew Newell
2011 年 7 月 20 日
0 投票
I can't think of a good way to use dynamic superclass names, and it sounds like bad programming practice to me.
Here is a different approach, which I'll describe using the names you provided. In your superclass, define demo_method so it does nothing but create intermediate_variables and feed them to another method ( e.g., demo_method_post ) that does all the work. You could make demo_method_post hidden. Any intermediate class could overload demo_method or demo_method_post, as convenient, and in turn your subclass can either overload them or use them as is. That gives you a lot of flexibility.
Andrew Newell
2011 年 7 月 21 日
How about just wrapping the call in its own method? If you'll forgive a little Monty Python whimsy, suppose your overloaded method is called snob and the class of interest is upperclass. Define a method upperclass_snob:
function obj = upperclass_snob(varargin)
obj = snob@upperclass(varargin{:});
end
Then all you have to change is the identity of upperclass.
3 件のコメント
Daniel Shub
2011 年 7 月 21 日
I am pretty sure you can only call a superclass method from a subclass method with the identical name.
Jared
2011 年 7 月 21 日
Alrik
2011 年 8 月 18 日
I like the "little Monty Python whimsy" though
カテゴリ
ヘルプ センター および File Exchange で Subclass Definition についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!