Having an instance of a class take on a value?

1 回表示 (過去 30 日間)
Chris Gnam
Chris Gnam 2019 年 8 月 24 日
コメント済み: Chris Gnam 2019 年 8 月 24 日
What I mean by this is I'd like to implement behavior similar to that of the datetime class.
When you create an instance of datetime, the resulting object shows up as:
foo =
datetime
24-Aug-2019 16:30:23
foo still maintains the fields:
'Format'
'TimeZone'
'Year'
'Month'
'Day'
'Hour'
'Minute'
'Second'
'SystemTimeZone'
But foo itself appears to have a "value" (which shows the day-month-year hour minute seconds values). This behavior is nice since it lets you do things like "foo1 - foo2" where both foo1 and foo2 are datetime objects and the resulting value is also a datetime.
I'm unsure of how to get this same behavior with a custom class of my own creation? I'm trying to implement my own time keeping class for astronomical purposes, so this kind of behavior would be extremely useful. I had originally attempted to use datetime as a superclass, however datetime appears to be sealed and so I cannot inherit from it unfortunately.
Any help would be greatly appreciated!

採用された回答

Steven Lord
Steven Lord 2019 年 8 月 24 日
  1 件のコメント
Chris Gnam
Chris Gnam 2019 年 8 月 24 日
Perfect! Thank you, overloading the operators was just what I was looking for. I just didn't know how to do that. Thanks so much!

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeSubclass Definition についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by