根据我的理解,有界多态是保证了子类的使用安全,不会返回同一个父类下的不同子类。
Sample代码如下:
abstract class QueryPlan[PlanType <: QueryPlan[PlanType]] extends TreeNode[PlanType] {}
这是QueryPlan的一个子类。
abstract class LogicalPlan
extends QueryPlan[LogicalPlan]
with AnalysisHelper
with LogicalPlanStats
with QueryPlanConstraints
with Logging {}
这是QueryPlan的一个子类。
abstract class SparkPlan extends QueryPlan[SparkPlan] with Logging with Serializable{}
这样的语义可以两个子类中的父类中的type变量绑定为子类自身的type,而不是其他子类的Type。
本文探讨了有界多态的概念及其在确保子类安全使用方面的作用。通过具体的Scala样例代码展示了如何限制泛型参数的类型,使得子类只能继承自指定的父类类型。
2万+

被折叠的 条评论
为什么被折叠?



