我正在使用一个类似于Product的库
class Product { def toString() = "Whatever" }
1 – Copy the contents of that class and make same new class in you own project and do whatever with that method now.
2 – UseScala Impilicit
第一种方法非常可悲.所以我尝试了第二个,但面对这个问题.我成功地在该类中添加了新方法但无法覆盖现有方法.让我用例子解释一下:
class NewProduct(val p: Product) { override def toString() = "an-other whatever" } implicit def customToString(p: Product) = new NewProduct(p)
现在,如果我以这种方式打印println((new Product()).toString)它打印任何东西,但我期待的其他什么
它似乎没有覆盖该方法,因为如果我添加新方法然后它按预期工作
class NewProduct(val p: Product) { def NewtoString() = "an-other whatever" } implicit def customToString(p: Product) = new NewProduct(p)
现在,如果我以这种方式打印println((new Product()).NewtoString)它会打印另一个 – 无论它的平均新方法NewtoString被添加到该类.
我错过了什么?
是否有可能在Scala中使用impicit覆盖方法?