フィルターのクリア

Is it possible to subclass Date?

2 ビュー (過去 30 日間)
Ricardo
Ricardo 2013 年 2 月 14 日
I have a class called myDate that represents dates in a special format, and I'd like to be able to define the year function for this class so that it returns the year associated with the date (similar to the functionality of the built-in function).
However, when I specify my code like this:
classdef myDate < Date
function y = year(obj)
% code to obtain the year
end
end
MATLAB gives me an error that states "'year' might be used imcompatibly or redefined."
I'm guessing this is because Date isn't technically a class (perhaps) and the year function is already used as a built-in function, but is there a way to implement this? The myDate object doesn't use a serial date or datevec as its underlying implementation, either, and I'm only using the base MATLAB.

採用された回答

Sean de Wolski
Sean de Wolski 2013 年 2 月 14 日
No you'll have to write your own. (The reason your getting the error above is because year needs to be inside a methods block.)
Of course you could make the myDate simply be a handle class with static methods year(), month() etc.
That way you don't have to create the object to use them.
y = myDate.year()
  2 件のコメント
Ricardo
Ricardo 2013 年 2 月 14 日
I have a methods block in my actual class, but I left it out of my code sample for conciseness. The error was happening because I already had a property of the class named year.
Sean de Wolski
Sean de Wolski 2013 年 2 月 14 日
ahh.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeConstruct and Work with Object Arrays についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by