在 Scala 中处理多个特征和配置文件组合的建议

问题描述 投票:0回答:1

我有一种情况,我有几个特征,每个特征都包含一组方法。我现在必须通过配置文件向外部世界公开这些功能。例如,基本配置文件将是另一个特征,但这仅包含特征 1 中的方法 a 和 2,特征 2 中的方法 4 和 5 等,我可以将这些配置文件创建为新特征并重复方法实现,但是我怎样才能重用我的特征中已经定义的方法并更优雅地创建配置文件特征呢?例如.,

trait InternalTrait1 {
  def method1(int: Int): Int
  def method2(int: Int): Int
}

trait InternalTrait2 {
  def method3(int: Int): Int
  def method4(int: Int): String
}

我现在想要:

trait BasicProfile {
  def method2(int: Int): Int
  dev method4(int: Int): String
}

我想分别引用Trait1和Trait2中的method2和method4。有任何想法吗?我想到了自我特质,但不确定这是否是一个好主意?

scala traits
1个回答
0
投票

您能否再介绍一些特征(例如下面的

Method2Doer
Method4Doer
)?

trait Method2Doer {
  def method2(int: Int): Int
}

trait Method4Doer {
  def method4(int: Int): String
}
  
trait InternalTrait1 extends Method2Doer {
  def method1(int: Int): Int
}

trait InternalTrait2 extends Method4Doer {
  def method3(int: Int): Int
}

trait BasicProfile extends Method2Doer with Method4Doer
© www.soinside.com 2019 - 2024. All rights reserved.