我正在使用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