scala - 如何在对象上调用 '!=' 方法?

标签 scala scala-2.8

我刚开始玩 scala,一直在使用 Michel Schinz (http://www.scala-lang.org/node/198) 的“Scala 实例”作为起点。在 traits 部分,我尝试使用反射/方法调用来测试 traits 并想测试所有的比较运算符。我遇到了名称修改的问题,并在 NameTransformer 中为运算符(operator)找到了解决方案。但是,在我看来,!= 运算符并没有转换为等效函数,如 <、<=、>、>=、等于。我想知道是否有一种方法可以像我尚未发现的其他运算符一样调用 !=?

来自pdf:

trait ord {
def < (that:Any):Boolean
def <= (that:Any):Boolean = (this < that) || (this == that)
def > (that:Any):Boolean = !(this <= that)
def >= (that:Any):Boolean = !(this < that)
}

class Date(y:Int, m:Int, d:Int) extends ord{
def year = y
def month = m
def day = d

override def toString():String = year + "-" + month + "-" + day

override def equals(that:Any): Boolean =
  that.isInstanceOf[Date] && {
    val o = that.asInstanceOf[Date]
    o.day == this.day && o.month == this.month && o.year == this.year
  }

override def <(that:Any):Boolean = {
  if (!that.isInstanceOf[Date])
    error("Cannot compare " + that + " and date")
  val o = that.asInstanceOf[Date]
  (year < o.year) ||
  (year == o.year && (month < o.month ||
    (month == o.month && day < o.day )))
}
}

我的代码:

def Classes_Traits(){
val (d1, d2, d3)  = (new Date(2001, 10, 1), new Date(2001, 10, 1), new Date(2000, 1, 10))
println("d1 : " + d1)
println("d2 : " + d2)
println("d3 : " + d3)


Array((d1,d2), (d2,d3), (d3,d1)).foreach {
  (comp:(Date, Date)) =>
  println("comparing " + comp._1 + " and " + comp._2)
  val d = comp._1.getClass()
  Map(
       "equals            " -> "equals",
       "not equals        " -> "!=",
       "less than         " -> "<",
       "less than or equal" -> "<=",
       "more than         " -> ">",
       "more than or equal" -> ">=").foreach {
     (a) =>
       println(a._1 + " : " + 
       d.getMethod(scala.reflect.NameTransformer.encode(a._2), 
                   classOf[Object]).invoke(comp._1, comp._2))
       }
   /*println("equals : " + m.invoke(comp._1, comp._2) )
   // Same as above
   println(comp._1 + " == " + comp._2 + " is " + (comp._1 == comp._2))
   println(comp._1 + " != " + comp._2 + " is " + (comp._1 != comp._2))
   println(comp._1 + " > " + comp._2 + " is " + (comp._1 > comp._2))
   println(comp._1 + " >= " + comp._2 + " is " + (comp._1 >= comp._2))
   println(comp._1 + " < " + comp._2 + " is " + (comp._1 < comp._2))
   println(comp._1 + " <= " + comp._2 + " is " + (comp._1 <= comp._2))
   */
  }
}

异常: 比较 2001-10-1 和 2001-10-1 大于或等于 : true 超过:假

Exception in thread "main" java.lang.NoSuchMethodException:    proj2.Main$Date.$bang$eq(java.lang.Object)
    at java.lang.Class.getMethod(Class.java:1605)
    at proj2.Main$$anonfun$Classes_Traits$1$$anonfun$apply$1.apply(Main.scala:180)
    at proj2.Main$$anonfun$Classes_Traits$1$$anonfun$apply$1.apply(Main.scala:178)
    at scala.collection.immutable.HashMap$HashMap1.foreach(HashMap.scala:125)
    at scala.collection.immutable.HashMap$HashTrieMap.foreach(HashMap.scala:344)
    at proj2.Main$$anonfun$Classes_Traits$1.apply(Main.scala:177)
    at proj2.Main$$anonfun$Classes_Traits$1.apply(Main.scala:168)
    at scala.collection.IndexedSeqOptimized$class.foreach(IndexedSeqOptimized.scala:34)
    at scala.collection.mutable.ArrayOps.foreach(ArrayOps.scala:35)
    at proj2.Main$.Classes_Traits(Main.scala:167)
    at proj2.Main$.main(Main.scala:26)
    at proj2.Main.main(Main.scala)

最佳答案

尝试调用 equals 并取反结果。 ==!= 得益于 Scala 中的一点编译器魔法(例如,您可以调用 null.==(4) 而无需获取NullPointerException).

关于scala - 如何在对象上调用 '!=' 方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6308373/

相关文章:

scala - 最佳功能方法

android - 使用 Scala 2.8 Trunk 构建以 Android 为目标

scala - Scala 中用于具有继承返回类型的集合的最小框架

scala - Akka Streams ActorRefSource 消息顺序

scala - 如何在 Ga特林 中从 csv 文件注入(inject)数据?

scala - 在dispatch-classic中使用cookie

scala - 如何在scala中将文件存储为数组?

scala - 在分配 "null"之前,必须限制类型是什么?

scala - 更简洁的类匹配和访问 Option[List] 的最后一个方法

scala - 使用 onComplete 时限制 Scala Future block