Regarding the significance of abstract classes in Matlab, is my understanding correct?"

조회 수: 5 (최근 30일)
fa wu
fa wu 2023년 11월 25일
편집: Matt J 2023년 11월 25일
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
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 CenterFile Exchange에서 Java Package Integration에 대해 자세히 알아보기

제품


릴리스

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by