Inheritance of classes inside a package

조회 수: 5 (최근 30일)
Petr Posik
Petr Posik 2011년 7월 25일
Hi,
I have run into problems when using class inheritance in combination with packages. I think it is the same issue as already asked in MATLAB newsgroup a few years ago.
First, suppose I have a bunch of files with the following structure:
./InnerClass.m
./OuterClass.m
and the following conents:
-- InnerClass.m :
classdef InnerClass
end
-- OuterClass.m :
classdef OuterClass < InnerClass
end
Now, being in the . directory, when I issue the command:
>> obj = OuterClass
I get the following answer:
obj =
OuterClass with no properties.
Methods, Superclasses
So far, so good, it works as expected.
Let's move the classes into a package, so that we would have the following structure:
./+package/InnerClass.m
./+package/OuterClass.m
Now, being in the . directory, the command
>> obj = OuterClass
ends up with an error. Of course, now the classes are part of package, we have to call it this way:
>> obj = package.OuterClass
But, GEEEEZ, I got another error message!!!
??? Error using ==> package.OuterClass
The specified super-class 'InnerClass' contains a parse error or cannot be found on MATLAB's search path, possibly shadowed by
another file with the same name.
QUESTION 1: Is this the expected behavior? If yes, what am I doing wrong???
Ok, so it seems that the OuterClass inside package somehow cannot see the definition of the InnerClass. Well, OK, so when I change the current working directory to be the +package directory and try to create the object from there, everything should work as in the beginning, right?
>> cd +package
>> obj = OuterClass
??? Undefined function or variable 'OuterClass'.
WHAT!?!?!
Question 2: Is this the expected behavior? Does the situation of the package directory being the working directory somehow change the way the source files are processed?
Question 3: I work in R2009a, is it the same in the newer versions?
I feel completely puzzled! These are really basic things and I do not understand them. Can somebody help me???
Thanks a lot,
Petr

채택된 답변

Titus Edelhofer
Titus Edelhofer 2011년 7월 25일
Hi Petr,
interesting, I haven't tried this. What works is to treat InnerClass as part of the package, i.e.
classdef OuterClass < package.InnerClass
Then it should work. Whether functions/classes inside the package should be "freed" from the necessity to do so, I don't know ... By the way: package directory should not be the working directory, that makes things more complicated then necessary.
Titus
  댓글 수: 1
Petr Posik
Petr Posik 2011년 7월 25일
Hi Titus.
Thanks for the answer, that works. But it also means that the OuterClass must know that it is placed in the |package|. Well, this is not unusual, in Java you have to specify the package of the current file using
package packagename;
But it still seems to me a bit strange that a file must know in what package(s) it is placed... :-(

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

추가 답변 (1개)

Daniel Shub
Daniel Shub 2011년 7월 25일
This seems to me to the documented (although maybe not expected) behavior.
Quesiton 1: The documentation of calling a package method within the package:
says that you must use the "package." notation.
Question 2: The documentation on "Packages and the MATLAB Path" is pretty clear:
that being in the package directory doesn't do much.
Question 3: The documentation is from r2011a, so yes, it is the same in the newer version.
  댓글 수: 3
Ron Lu
Ron Lu 2013년 5월 22일
That is lame. If I write my classes first, then separate them into different packages, I have to go back to add package name dot (or import *) anywhere the class is referred.
Daniel Shub
Daniel Shub 2013년 5월 23일
@Ron, I would suggest you file an enhancement request if you would like to see a change.

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

카테고리

Help CenterFile Exchange에서 Whos에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by