scala Scala抽象案例类
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9367950/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Scala abstract case class
提问by Saar
Given any CardI would like to be able to get the next and the previous Cardsif they exist.
鉴于任何Card我希望能够获得下一个和上一个,Cards如果它们存在的话。
The idea with the next()and prev()functions is that they returns the next or previous Suits, respectively. The order of the suits to be: Hearts, Spades, Diamonds, Clubs.
next()和prev()函数的想法是它们分别返回 next 或 previous Suits。花色的顺序是:Hearts, Spades, Diamonds, Clubs。
For more clarification, I should do these
为了更清楚,我应该做这些
// case class Card(n : Int, s : Suit)
// 1. define abstract case class Suit
// 2. define abstract case classes RedSuit and BlackSuit
// 3. define concrete case classes Hearts, Spades, Diamonds, Clubs
// 5. define abstract methods next():Suit and prev():Suit on Suit class
// 6. implement the next():Suite and prev():Suit on each of the Hearts, Diamonds,Spades and Clubs classes
// 7. implement the next():Card and prev():Card on the Card class
But in the first place I can't implement the next()on Hearts class
但首先我无法实现next()on Hearts 类
case class Card(n: Int, s: Suit)
abstract case class Suit{
type cardsuit <: Suit
def next(): cardsuit
def prev(): cardsuit
}
abstract case class RedSuit extends Suit {
type red <: RedSuit
}
abstract case class BlackSuit extends Suit {
type black <: BlackSuit
}
case class Hearts extends RedSuit {
type red = Hearts
def next(): Spade = ??? // I wanna have Spades hier
def prev(): Club = ??? // I wanna have Clubs hier
}
回答by Jean-Philippe Pellet
Not exactly sure what you're trying to do… In any case, case classes shouldn't be subclassed by other case classes (the compiler should even warn you about this).
不完全确定您要做什么...无论如何,案例类不应该被其他案例类子类化(编译器甚至应该警告您这一点)。
Regarding modeling your suits, how about something like this?
关于给你的西装建模,这样的事情怎么样?
trait Suit {
type ThisSuit <: Suit
type PrevSuit <: Suit
type NextSuit <: Suit
def prev: PrevSuit
def next: NextSuit
}
trait RedSuit extends Suit {
type ThisSuit <: RedSuit
}
trait BlackSuit extends Suit {
type ThisSuit <: BlackSuit
}
case object Hearts extends RedSuit {
type ThisSuit = Hearts.type
type PrevSuit = Nothing
type NextSuit = Spades.type
def prev = throw new NoSuchElementException
def next = Spades
}
case object Spades extends BlackSuit {
type ThisSuit = Spades.type
type PrevSuit = Hearts.type
type NextSuit = Diamonds.type
def prev = Hearts
def next = Diamonds
}
case object Diamonds extends RedSuit {
type ThisSuit = Diamonds.type
type PrevSuit = Spades.type
type NextSuit = Clubs.type
def prev = Spades
def next = Clubs
}
case object Clubs extends BlackSuit {
type ThisSuit = Clubs.type
type PrevSuit = Diamonds.type
type NextSuit = Nothing
def prev = Diamonds
def next = throw new NoSuchElementException
}
You may want prevand nextto return Option[PrevSuit]and Option[NextSuit], respectively, rather than throwing exceptions; or make the suits wrap around between Hearts and Clubs.
您可能希望prevand分别next返回Option[PrevSuit]and Option[NextSuit],而不是抛出异常;或者让西装环绕在 Hearts 和 Clubs 之间。

