Regarding the significance of abstract classes in Matlab, is my understanding correct?"
5 次查看(过去 30 天)
显示 更早的评论
In C++, runtime polymorphism requires the presence of abstract classes or interfaces.
However, in Matlab, achieving polymorphism doesn't necessitate abstract classes; it can be directly implemented.It seems that abstract classes/interfaces in Matlab are not as indispensable as they are in C++.I speculate that the 'primary purpose' of abstract classes in Matlab is for collaborative work among team members.
For example, if the team leader does not specify the abstract class 'Pet,' the member writing the 'Dog' class may not implement the 'eat' method, and the member writing the 'Cat' class may not have a 'jump' method. However, if the team leader defines an abstract class 'Pet' and declares the 'eat' and 'jump' methods within it, specifying that the 'Dog' and 'Cat' classes must inherit from 'Pet,' the team members writing the 'Dog' and 'Cat' classes must then implement the 'eat' and 'jump' methods. Otherwise, the code will result in an error.Other than that, abstract classes in Matlab serve no other purpose
----------------Could you please confirm if my above speculation is correct?
1 个评论
Matt J
2023-11-25
编辑:Matt J
2023-11-25
In C++, runtime polymorphism requires the presence of abstract classes or interfaces.
I'm not sure that's true. ChatGPT certainly doesn't think so (not to say that that's gospel):
"Polymorphism relies on the use of virtual functions, and an abstract class is one way to ensure that these functions are declared in the base class and overridden in derived classes. However, you can achieve polymorphism in C++ without an abstract class by using virtual functions in a non-abstract base class and overriding them in derived classes. The key is to use pointers or references of the base class type to achieve dynamic dispatch at runtime."
回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Java Package Integration 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!