简体   繁体   English

从基本特征访问伴侣对象

[英]Access companion object from base trait

If I define a trait, I can access successor's fields. 如果定义了特征,则可以访问后继字段。 But what if one of those fields is in companion object (eg for speed, as static member), can I access it too? 但是,如果这些字段之一位于伴随对象中(例如,为了提高速度,作为静态成员),我也可以访问它吗?

Is there a relationship between TheTrait object and TheSuccessor object, as with TheTrait trait and TheSuccessor class? TheTrait对象和TheSuccessor对象之间是否存在关系,就像TheTrait特性和TheSuccessor类一样?

As far as I can tell you cant. 据我所知你不能。 You could either let the companion object implement the trait, or maybe define an abstract method in the trait. 您可以让伴侣对象实现特征,也可以在特征中定义一个抽象方法。 Implementing classes could then provide access to the companion object by implementing the method. 然后,实现类可以通过实现方法来提供对伴随对象的访问。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM