0

我在一个项目中包含 3 个项目,我们称它们为project12并且3它们包含在main project. 1一个是库,而其他two的是控制台可执行项目。

object Application现在我在项目中2有两个几乎相同的3

package com.project1

abstract class Class123 extends Actor { ... }
abstract class Class456 { ... }

package com.project2

class Class123  extends package1.Class123 { ... }
class Class456  extends package1.Class456 { ... }

object Application extends App {
  val system = ActorSystem()
  val myActor = system.actorOf(Props[Class123])
  val b = new Class456               
  // some actions
}

package com.project3

class Class123  extends package1.Class123 { ... }
class Class456  extends package1.Class456 { ... }

object Application extends App {
  val system = ActorSystem()
  val myActor = system.actorOf(Props[Class123])
  val b = new Class456            
  // some actions
}

如何减少重复次数?我可以创建object Applicationin package1,但它应该是通用的(Scala 不允许这样做),我必须从它继承(Scala 也不允许这样做)。

我该怎么办?

4

1 回答 1

1

使它成为一个类而不是对象:

class MyApp[C123, C456] extends App {
  val system = ...
  val myActor = ...
  val b = ...            
}

package com.project1
object Application1 extends MyApp[com.project1.Class123, com.project1.Class456] { 
  ... 
}

package com.project2
object Application2 extends MyApp[com.project2.Class123, com.project2.Class456] { 
  ... 
}
于 2013-09-13T06:09:45.057 回答