Scala 基本特征是什么
只是在做一个概念验证库。尝试尽可能地抽象和混合特征,而不是继承,同样,在这个库中,所有角色实例之间实际上只有一个共同特征,根本没有类对象,只是混合特征来构建case对象。试图制作一个智能的ish-toString实现,它识别出我加入的特性,并相应地改变toString。我目前的理论是这样的:Scala 基本特征是什么,scala,Scala,只是在做一个概念验证库。尝试尽可能地抽象和混合特征,而不是继承,同样,在这个库中,所有角色实例之间实际上只有一个共同特征,根本没有类对象,只是混合特征来构建case对象。试图制作一个智能的ish-toString实现,它识别出我加入的特性,并相应地改变toString。我目前的理论是这样的: //Completely wrong, just a rough pseudocode of one potential ideal implicit class CharImpl[A <: Char
//Completely wrong, just a rough pseudocode of one potential ideal
implicit class CharImpl[A <: Character](a: A){
def toString:String = {
for(
i ← _:HasName ⇒ s"Name: ${a.name}"
j ← _:HasAbbreviation ⇒ s"Abbreviation: ${a.castTo[HasAbbreviation].abbreviation}"
k ← _:HasUnicode ⇒ s"Unicode: ${a.castTo[HasUnicode].unicode}"
l ← _:HasDecimal ⇒ s"Decimal: ${a.castTo[HasDecimal].decimal}"
) {(i,j,k,l) mkString "\n"}
}
}
//完全错误,只是一个潜在理想的粗略伪代码
隐式类CharImpl[A如何使用super.toString
加上一些新的内容来重写toString
trait A { override def toString: String = super.toString + " with A" }
trait B { override def toString: String = super.toString + " with B" }
trait C { override def toString: String = super.toString + " with C" }
class D { override def toString: String = super.toString + " D" }
scala> new D with C with A
res0: D with C with A = $anon$1@4df5014e D with C with A
scala> new D with C with B
res1: D with C with B = $anon$1@29042f37 D with C with B
class E extends C with A { override def toString: String = "E " + super.toString }
scala> new E
res3: E = E E@40145d8e with C with A
使用super.toString
加上一些新的内容覆盖toString
怎么样
trait A { override def toString: String = super.toString + " with A" }
trait B { override def toString: String = super.toString + " with B" }
trait C { override def toString: String = super.toString + " with C" }
class D { override def toString: String = super.toString + " D" }
scala> new D with C with A
res0: D with C with A = $anon$1@4df5014e D with C with A
scala> new D with C with B
res1: D with C with B = $anon$1@29042f37 D with C with B
class E extends C with A { override def toString: String = "E " + super.toString }
scala> new E
res3: E = E E@40145d8e with C with A
使用super.toString
加上一些新的内容覆盖toString
怎么样
trait A { override def toString: String = super.toString + " with A" }
trait B { override def toString: String = super.toString + " with B" }
trait C { override def toString: String = super.toString + " with C" }
class D { override def toString: String = super.toString + " D" }
scala> new D with C with A
res0: D with C with A = $anon$1@4df5014e D with C with A
scala> new D with C with B
res1: D with C with B = $anon$1@29042f37 D with C with B
class E extends C with A { override def toString: String = "E " + super.toString }
scala> new E
res3: E = E E@40145d8e with C with A
使用super.toString
加上一些新的内容覆盖toString
怎么样
trait A { override def toString: String = super.toString + " with A" }
trait B { override def toString: String = super.toString + " with B" }
trait C { override def toString: String = super.toString + " with C" }
class D { override def toString: String = super.toString + " D" }
scala> new D with C with A
res0: D with C with A = $anon$1@4df5014e D with C with A
scala> new D with C with B
res1: D with C with B = $anon$1@29042f37 D with C with B
class E extends C with A { override def toString: String = "E " + super.toString }
scala> new E
res3: E = E E@40145d8e with C with A
我从来不知道super调用了所有toString方法。我以为它只调用了1,除非指定。谢谢你的帮助。我最后稍微修改了一下,但效果很好!我从来不知道super调用了所有toString方法。我以为它只调用了1,除非指定。谢谢你的帮助。我最后稍微修改了一下,但效果很好!我从来都不知道super调用了所有toString方法。我以为它只调用了1,除非指定。谢谢你的帮助。我最后稍微修改了一下,但效果很好!我从来都不知道super调用了所有toString方法。我以为它只调用了1,除非指定。谢谢你的帮助。我最后稍微修改了一下,但效果很好。greaT