我刚刚开始玩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)
答案 0 :(得分:5)
尝试调用equals
并取消结果。 ==
和!=
受益于Scala中的一点编译器魔法(例如,您可以在不获取null.==(4)
的情况下调用NullPointerException
)。