如何在一个班级中多次使用一个特征? [英] How to use a trait several times in a class?

查看:21
本文介绍了如何在一个班级中多次使用一个特征?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码:

trait T {
    function foo() {}
}

class C {
    use T { T::foo as bar; }
    use T { T::foo as baz; }
}

产生以下错误:

Trait 方法 bar 没有被应用,因为有碰撞与 C 上的其他 trait 方法

Trait method bar has not been applied, because there are collisions with other trait methods on C

可以在一个班级中两次使用一个特征吗?

Is it possible to use a trait twice in a class?

推荐答案

要多次导入"在 trait 中定义的具有不同名称的方法,请执行以下操作:

To "import" a method defined in a trait multiple times with different names do this:

class C {
  use T {
    foo as bar;
    foo as baz;
  }
}

这篇关于如何在一个班级中多次使用一个特征?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆