我正在使用Scala match/case
語句來匹配給定java類的接口。我希望能夠檢查一個類是否實現了接口的組合。我似乎得到這個工作的唯一方法是使用嵌套的match/case
陳述,這看起來很醜。匹配Java接口時的Scala匹配/ case語句
可以說我有一個PersonImpl對象,它實現了Person,Manager和Investor。我想看看PersonImpl是否實現了經理和投資者。我應該可以做到以下幾點:
person match {
case person: (Manager, Investor) =>
// do something, the person is both a manager and an investor
case person: Manager =>
// do something, the person is only a manager
case person: Investor =>
// do something, the person is only an investor
case _ =>
// person is neither, error out.
}
case person: (Manager, Investor)
只是不起作用。爲了得到它的工作,我必須做到以下看起來很醜陋。
person match {
case person: Manager = {
person match {
case person: Investor =>
// do something, the person is both a manager and investor
case _ =>
// do something, the person is only a manager
}
case person: Investor =>
// do something, the person is only an investor.
case _ =>
// person is neither, error out.
}
這簡直太難看了。有什麼建議麼?
'情況的人:經理Investor'應該工作。當你對這些對象調用一個'Investor'方法時會發生什麼,你說的只是'Manager'? – 2011-05-02 20:14:22
如果您可以接受答案或告訴我們您想知道關於此主題的其他信息,那將是非常好的。 – 2011-05-24 09:42:55