Here's a little fun class:
abstract class Concept {
late Enum option;
String get name => option.name;
}
and you might implement it like this:
enum FeeOption {
fast,
standard,
slow,
}
class FastFeeRate extends Concept {
FeeOption option = FeeOption.fast;
}
print(FastFeeRate().name); // 'fast'
but then you get an error:
FastFeeRate.option=' ('void Function(FeeOption)') isn't a valid override of 'Concept.option=' ('void Function(Enum)').
So, how do you specify a variable as any kind of enum, not Enum itself?
1条答案
按热度按时间ddrv8njm1#
Your class
Concept
has a mutable (late
, but that doesn't matter) field with typeEnum
. That means it has a setter namedoption=
with an argument type ofEnum
.The subclass
FastFeeRate
is a subclass. It has another field (your class has two fields!) also namedoption
, which has a setter with an argument type ofFastFeeRate
.That's not a valid override. The subclass setter must accept all arguments that the superclass setter does, but it doesn't accept all
Enum
values.What you might have intended to do is:
or
depending on whether you want to define the field in the superclass or the subclass (but make sure to only define a concrete field in one of them).