function - 在 Scala 中从其他类导入函数

标签 function class scala import

比如说我有

class foo{
  def bar = 7
}

class qaz{
  //Here I want to have something like: val foobar = bar
  //What I don't want to have is val foobar = (new foo).bar   
}

我怎样才能做到这一点?

最佳答案

您可以使用 foo 的伴生对象定义 bar .

然后你可以简单地将它导入 qaz :

// in foo.scala
object foo {
  def bar = 7
}
class foo {
  // whatever for foo class
}

// in qaz.scala
import mypackage.foo.bar
class qaz {
  val foobar = bar     // it works!
}

关于function - 在 Scala 中从其他类导入函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24784162/

相关文章:

javascript - 在 Javascript 类中声明公共(public)方法时出现语法错误

java - 在 Scala 的 "Future"的 ForkJoinPool 中,为什么 worker 的 ID 总是奇数?

scala - 展平 RDD 中的 Scala 映射

python - python字典中的函数

c++ - 如何读取位和将位写入字节数组

PHP变量没有传递给函数

c++ - 使用带有类和构造函数的虚函数来打印书籍信息

java - Hbase 使用值列表过滤列

php - 已弃用的 PHP 函数的替代方法 : eregi_replace

javascript - 访问 jquery 选择器内的函数