Is it possible to subclass Date?

2 次查看(过去 30 天)
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.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Construct 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