필터 지우기
필터 지우기

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 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