Groovy のユーザー ガイドに例があります。
@Category(Vehicle) class FlyingAbility {
def fly() { "I'm the ${name} and I fly!" }
}
@Category(Vehicle) class DivingAbility {
def dive() { "I'm the ${name} and I dive!" }
}
interface Vehicle {
String getName()
}
@Mixin(DivingAbility)
class Submarine implements Vehicle {
String getName() { "Yellow Submarine" }
}
@Mixin(FlyingAbility)
class Plane implements Vehicle {
String getName() { "Concorde" }
}
@Mixin([DivingAbility, FlyingAbility])
class JamesBondVehicle implements Vehicle {
String getName() { "James Bond's vehicle" }
}
のインスタンスがある場合JamesBondVehicle
:
def vehicle = new JamesBondVechile()
vehicle
飛行能力があることを確認するにはどうすればよいですか?特に、車両がどのように作成されたかわからない場合。
以下は機能しません。
assert vehicle instanceof FlyingAbility
以下の作品:
assert vehicle.respondsTo('fly')
しかし、これはミックスインの存在を確認するための一般的な方法ではありません。