为什么在方法未被覆盖时使用“协变”?
从这里:
bool shouldReclip(covariant CustomClipper<T> oldClipper);
AFAIK,covariant当您重写一个方法时,会使用该关键字,明确告诉分析器您将为其提供有效类型。但是在CustomClipper课堂上,没有这样的覆盖,那么为什么covariant要使用呢?
回答
您可以将参数标记为covariant超类中的参数,然后子类covariant也会自动标记其参数。
如果打算扩展类,这可以是子类作者的服务(和文档),以便他们知道应该协变地使用参数。
如果您查看这个子类,您会看到它们声明:
@override
bool shouldReclip(_DecorationClipper oldClipper) {...}
这是协变覆盖参数类型CustomClipper<Path>with _DeclarationClipper(which implements CustomClipper<Path>),他们不必covariant在这里写,因为超类为他们声明了它。