Accessing subclass methods from abstract superclass

조회 수: 40 (최근 30일)
thitch
thitch 2020년 12월 23일
댓글: thitch 2020년 12월 29일
Hi all,
Longtime listener, first time caller. I'm attempting to augment an abstract superclass with a method that calls (concrete) methods from a subclass. In the example below, I would like to call ChildClass.doC() and see, in the Command Window:
Child class, doA()
Child class, doB()
Here is my MWE.
classdef (Abstract) ParentClass
%PARENTCLASS Testing properties of abstract classes
methods (Abstract = true, Static = true)
doA();
doB();
end
methods (Static = true)
function doC()
doA();
doB();
end
end
end
classdef ChildClass < ParentClass
%CHILDCLASS Testing properties of abstract classes
methods (Static = true)
function doA()
disp('Child class, doA()')
end
function doB()
disp('Child class, doB()')
end
end
end
Upon the call to ChildClass.doC(), what I would like is for MATLAB to call ParentClass.doC(), realize that doA() and doB() are abstract methods, and go back `up' the call chain to ChildClass.doA(), ChildClass.doB(). I've been told this is a construct in C++, not sure if its possible here.
I'm doing this because I have many classes that inherit from ParentClass, and each implement their own version of doA(), doB(). These functions are typically used together, and I would like to make a `shorthand' a. la doC() in the parent class without having to write a seperate doC() method for each subclass. Thanks.

채택된 답변

Steven Lord
Steven Lord 2020년 12월 25일
classdef (Abstract) ParentClass
%PARENTCLASS Testing properties of abstract classes
methods (Abstract = true, Static = true)
doA();
doB();
end
methods (Static = true)
function doC()
doA();
doB();
end
end
end
This doesn't do what you think. Normally Static methods are called using the name of the class that defines them, though they can be called on class instances like non-Static methods. Since doC is Static and accepts no inputs, it can only be called via a class name.
Nothing in the code you've written makes it possible to call any class methods doA or doB. They are not called as Static methods with a class name, nor do they accept any inputs. So there's nothing telling MATLAB it should call the methods of either ParentClass or ChildClass. Unless there are functions doA and doB (not methods) ParentClass.doC() would error.
If you use per isakson's approach, doA and doB probably shouldn't be Static in either ParentClass or ChildClass.
  댓글 수: 1
thitch
thitch 2020년 12월 29일
This makes sense. In my actual application doC() does accept an input (an N x N matrix with a recognizable structure). I could write a try-catch statement for this input, however I would need to add an additional case for every new subclass. This seems like a pain going forward.. I was really looking for a solution in which doC() could identify the class type of the subclass from which it was invoked, but it sounds as though this isn't possible. Thanks!

댓글을 달려면 로그인하십시오.

추가 답변 (3개)

per isakson
per isakson 2020년 12월 24일
This is not what you ask for, but close. (I don't think what you want is possible with Matlab.)
>> child = ChildClass();
>> child.doC
Child class, doA()
Child class, doB()
where
classdef ParentClass
%PARENTCLASS Testing properties of abstract classes
methods (Abstract = true, Static = true)
doA();
doB();
end
methods % (Static = true)
function doC(this)
this.doA();
this.doB();
end
end
end
and
classdef ChildClass < ParentClass
%CHILDCLASS Testing properties of abstract classes
methods (Static = true)
function doA(~)
disp('Child class, doA()')
end
function doB(~)
disp('Child class, doB()')
end
end
end
  댓글 수: 1
thitch
thitch 2020년 12월 29일
Thank you for your answer. Unfortunately it's important that I keep these methods static. Each subclass defines a set of `rules' for matrix operations (in my application, matrix Lie groups), and I'd like to be able to operate on Matlab matrices without instantiating any objects.

댓글을 달려면 로그인하십시오.


Catalytic
Catalytic 2020년 12월 25일
classdef (Abstract) ParentClass
%PARENTCLASS Testing properties of abstract classes
methods (Abstract = true, Static = true)
doA();
doB();
end
methods (Static = true)
function doC()
ChildClass.doA();
ChildClass.doB();
end
end
end
  댓글 수: 1
thitch
thitch 2020년 12월 29일
Thank you for your answer. Please see my response to per isakson, above.

댓글을 달려면 로그인하십시오.


Matt J
Matt J 2020년 12월 25일
편집: Matt J 2020년 12월 25일
Probably not exactly what you were hoping for, but this solution does make sense when doC@ParentClass contains a lot of code. Note that doC@ChildClass is very short and can be simply copy/pasted as is to any other child class that you make.
classdef (Abstract) ParentClass
%PARENTCLASS Testing properties of abstract classes
methods (Abstract = true, Static = true)
doA();
doB();
end
methods (Static = true)
function doC(name)
doA=str2func(name+".doA");
doA=str2func(name+".doB");
doA();
doB();
end
end
end
classdef ChildClass < ParentClass
%CHILDCLASS Testing properties of abstract classes
methods (Static = true)
function doA()
disp('Child class, doA()')
end
function doB()
disp('Child class, doB()')
end
function doC()
doC@ParentClass(mfilename)
end
end
end
  댓글 수: 1
thitch
thitch 2020년 12월 29일
Thank you for your answer. This is indeed a workable solution, however it's approximately the same amount of work as creating a separate doC() method for each subclass (the variation involved between subclasses is very small). I was really looking to learn if there was a `one-shot' solution involving only the parent class. Great to learn about str2func() though!

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Construct and Work with Object Arrays에 대해 자세히 알아보기

제품


릴리스

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by