Borrar filtros
Borrar filtros

Is it possible to subclass Date?

2 visualizaciones (últimos 30 días)
Ricardo
Ricardo el 14 de Feb. de 2013
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.

Respuesta aceptada

Sean de Wolski
Sean de Wolski el 14 de Feb. de 2013
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 comentarios
Ricardo
Ricardo el 14 de Feb. de 2013
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 el 14 de Feb. de 2013
ahh.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Construct and Work with Object Arrays en Help Center y File Exchange.

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by