フィルターのクリア

Is it possible to seal a class constructor method

3 ビュー (過去 30 日間)
Eivind Hennestad
Eivind Hennestad 2023 年 8 月 24 日
コメント済み: Eivind Hennestad 2023 年 8 月 29 日
I want to create a base class with a sealed constructor method, but even if I add Sealed to the method attributes of the methods block where the contructor is defined, MAATLAB allows me to override the class constructor in a subclass. I did not find any description of this in the documentation, but is this intended behavior, and is there no way to seal a class constructor?

採用された回答

Matt J
Matt J 2023 年 8 月 24 日
編集済み: Matt J 2023 年 8 月 24 日
Constructors are always sealed innately. The child class method that you've created with the same name as the parent constructor will not behave as a re-implementation of the constructor. It will behave as a converter.
  3 件のコメント
Matt J
Matt J 2023 年 8 月 24 日
編集済み: Matt J 2023 年 8 月 24 日
That is also true, since without a subclass constructor there is no way to create instances of the subclass.
However, it is unrelated to your posted question. Your posted question asks about sealing the parent constructor. Sealing a method prevents it from being overloaded in child classes. Perhaps you are thinking of the child class constructor as an overload of the parent class constructor. That is not correct. An overload of a parent class method in a subclass must have the same name as the parent class method. Conversely, parent and subclass constructors will always have different names.
Eivind Hennestad
Eivind Hennestad 2023 年 8 月 29 日
I was actually creating classes with the same names (different package prefixes) so my mind made the parallell with overloading of regular methods, but yes, of course - constructor methods usually have different names!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeNumeric Types についてさらに検索

製品


リリース

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by