【问题标题】:How do I invoke the '!=' method on an object?如何在对象上调用 '!=' 方法?
【发布时间】:2011-09-12 14:18:30
【问题描述】:

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

来自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 大于或等于:真 多于:假

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)

【问题讨论】:

    标签: scala scala-2.8


    【解决方案1】:

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

    【讨论】:

    • 谢谢。我将阅读更多关于特别编译器注意 ==!= 的内容。欢迎指出在哪里阅读它们。
    • 我想说,对于这些问题,编译小型 sn-ps 并查看生成的字节码是理解编译器的最佳方式。或者,查看编译器的源代码……
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-06-26
    • 1970-01-01
    • 1970-01-01
    • 2010-12-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多