Java 方法需要具有多重继承的参数
我不知道为什么我在网上找不到这个问题的答案。
我有实现多种方法的类,我想编写期望它们的方法。我不知道该怎么做,或者是否可能。
例如:
public void yellAtPet(<? extends Pet implements YellableAt> arg) {
arg.yellAt("Don't go there!");
arg.pet("Good Boy");
}
I don't know why I can't find an answer to this online.
I have classes that implement multiple methods and I would like to write methods to expect them. I don't know how to do it though or if it's even possible.
E.g:
public void yellAtPet(<? extends Pet implements YellableAt> arg) {
arg.yellAt("Don't go there!");
arg.pet("Good Boy");
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
扩展用于接口和父类。
如果你想强制多个扩展,你需要类似的东西:
要在方法上强制执行此操作,请泛化该类:
Extends is used for both interfaces and parent classes.
If you want to inforce multiple extends you needs something like:
To enforce this on a method, generify the class:
这应该可以作为通用方法正常工作,而无需使整个类通用:
This should work fine as a generic method, without making your entire class generic: