我可以将 Scala class 中的方法作为 var 引用吗?

Can I reference a Method from a Scala class as a var?

我正在尝试在 Scala var 中存储来自 2 个单独的 Java 类 的非静态或静态方法。我应该怎么做?

类似这样

// java code below
public class Class1 {
   public Static int f(int n) {
      return n;
   }
}

public class Class2 {
   public Class2() {}
   public int f(int n) {
      return n + 1;
   }
}

// pseudocode of the scala code below
object Main {
   var someFunction = _ // How do I typecast this?

   def main( ... ) {
      something match {
         case Some(_) =>
            someFunction = Class1.f // How do I set this?
         case None =>
            Object2 = new Class2
            someFunction = Object2.f // How do I set this?
      }
      someFunction(1)
   }
}

好的,我在搞乱 Scala 命令行的同时找到了实现它的方法

object O1 {
   // the static method
   def f(n: Int): Int = {
      return n
   }
}

class O2(m: Int) {
   // the nonstatic method
   def f(n: Int): Int = {
      return n + m
   }
}

// the _ is to explicitly show that the function type is expected
// Int => Int is the type casting for a function that takes an Int and returns an Int
var f: Int => Int = O1.f _

// Constructs an O2 and gets f
var f: Int => Int = (new O2(1)).f _