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!