Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
不久前,我发现了一个有趣的博客条目,其中有人展示了如何确保对象具有特定的行为。现在我想这样做,但我找不到博客。任何人都可以告诉我语法吗?
你是说结构类型吗?这是一个例子:
class Duck { def quack = "quack" } class OtherDuck { def quack = "mooh" } object Test { def testQuack (d: {def quack: String}) = d.quack }
然后你可以这样做:
Test.testQuack(new Duck) Test.testQuack(new OtherDuck)