How to import class definitions for typing
조회 수: 28 (최근 30일)
이전 댓글 표시
Hello,
I have a class that's composed of several other classes. How are you supposed to import additional definitions in the properties section of the class? For example
classdef SampleClass
properties
class1 (1,1) OtherClass1
class2 (1,1) OtherClass2
end
end
I tried the obvious things: before the classdef, even in the classdef and properties. None of which seem to work. I hope there's something I missing!
It's a bit frustrating since obviously in other languages this is trivial. I'm trying to be amendable to different ways of doing things, so if using namespaces is not the way to organize code please let me know.
Regards,
Isaac
댓글 수: 2
Steven Lord
2023년 2월 9일
Assuming you have classes named OtherClass1 and OtherClass2 in files OtherClass1.m and OtherClass2.m that are not in package directories, I would expect what you've written to work. Though there are a few caveats particularly if those classes are handle classes; see this documentation page and this one.
Can you say a little more about what difficulties you're encountering in your code, particularly with a more complete example? If you see error or warning messages the full and exact text of those messages would be useful as well.
채택된 답변
Steven Lord
2023년 2월 11일
The full name of that class is not OtherClass1. It is subdir.OtherClass1. This insulates you from the possibility that some other package directory has its own class in a file OtherClass1.m that you may or may not be aware of.
I had the statement "that are not in package directories" in my previous comment, but your class is in a package directory.
FYI, in the older object system classes were required to be in directories whose names start with the @ symbol followed by the name of the class. In the classdef-based object system this is no longer a requirement (unless you want to define your class methods across multiple files, as stated on this documentation page.) So you could have your OtherClass1.m directly in the +subdir directory if you wanted. Similarly you could have SampleClass in the same directory as the +subdir directory. This flattens your directory structure a little bit.
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Software Development Tools에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!