what I did:
extension BigDate on DateTime {
String get locatedWeekDay {
switch (weekday) {
case DateTime.sunday:
return "Sun";
case DateTime.monday:
return "Mon";
case DateTime.tuesday:
return "Tue";
......
default:
throw Exception();
}
}
}
class JapanDate extends DateTime {
@override
String get locatedWeekDay {
switch (weekday) {
case DateTime.sunday:
return "日";
case DateTime.monday:
return "月";
......
default:
throw Exception();
}
}
}
now I just run this:
DateTime d = JapanDate(2022, 3, 2);
print(d.locatedWeekDay);
it returns me "Wed" oh, can you help me to fix it?
I tried: to add @override to the get method, add the import to the first line.
CodePudding user response:
Just don't cast it to DateTime
leave it as JapanDate
and the code above should work as you expect it to.
JapanDate d = JapanDate(2022, 3, 2);
print(d.locatedWeekDay);
CodePudding user response:
There is no locatedWeekDay
method on DateTime
. Therefore, it can't be overridden, but you can create a custom class extending this or use an extension as you did for BigDate
.
To create a custom class, you need to pass data to super class.
class JapanDate extends DateTime {
JapanDate(int year,
[int month = 1,
int day = 1,
int hour = 0,
int minute = 0,
int second = 0,
int millisecond = 0,
int microsecond = 0])
: super(
year,
month = month,
day = day,
hour = hour,
minute = minute,
second = second,
millisecond = millisecond,
microsecond = microsecond);
String get locatedWeekDay {
switch (this.weekday) {
case DateTime.sunday:
return "日";
case DateTime.monday:
return "月";
//....
default:
return "N";
}
}
}
Now you can use JapanDate
class with locatedWeekDay
.
JapanDate d = JapanDate(2022, 3, 7);
print(d.locatedWeekDay); ///月
About .weekday
on DateTime
. It is defined as
external int get weekday;
External Functions An external function is a function whose body is provided separately from its declaration. An external function may be a top-level function (17), a method
You can follow this What does external mean in Dart?