![](/img/trans.png)
[英]How to pass generic Object as a Generic parameter on other method in java?
[英]how to pass a method as parameter of other method but making the last method have a generic as parameter on its signature?
我是泛型的新手,現在我才發現這一點。
我有辦法
//interface
myMethod(MyOtherService service)
//implementation
@Override
public myMethod(MyOtherService service){
service.doSomething();
}
我必須澄清MyFunction不會擴展其他類
我想做這個
//interface
myMethod(GenericFunction function)
//Implementation
@Override
myMethod(GenericFunction function){
//try to cast it to MyOtherService or ServiceABC, etc
if(function instanceof(MyOtherService.class)){
//convert and call to doSomething();
}
}
我確實做到了,編譯器沒有抱怨,問題是我不能用
我想使用功能接口(如果有),但是我的問題是doSomething()實際上接收了多個參數。
myMethod(myService)
因為它期待一個Function實例。
我在做什么錯? 任何想法?
我認為您只是使用Object作為參數,請參見以下代碼,
public class system_property implements A {
public static void main(String[] args) {
new system_property().myMethod(new MyOtherService());
}
@Override
public void myMethod(Object service) {
if (service instanceof MyOtherService) {
System.out.println("YES");
}
}
}
interface A {
void myMethod(Object service);
}
class MyOtherService {
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.