特质
trait
是为未知类型 Self
定义的一组方法集合。这些方法可以访问同一 trait 中声明的其他方法。
trait 可以为任何数据类型实现。在下面的例子中,我们定义了 Animal
,一组方法的集合。然后为 Sheep
数据类型实现 Animal
trait,这样就可以对 Sheep
使用 Animal
中的方法。
Press ← or → to navigate between chapters
Press S or / to search in the book
Press ? to show this help
Press Esc to hide this help
trait
是为未知类型 Self
定义的一组方法集合。这些方法可以访问同一 trait 中声明的其他方法。
trait 可以为任何数据类型实现。在下面的例子中,我们定义了 Animal
,一组方法的集合。然后为 Sheep
数据类型实现 Animal
trait,这样就可以对 Sheep
使用 Animal
中的方法。