【发布时间】:2012-01-15 16:29:07
【问题描述】:
为了减少类的依赖,我想将参数(使用泛型类)发送到扩展某些类并实现接口的构造函数,例如
public interface SomeInterface{
public void someMethod();
}
public class MyFragment extends Fragment implements SomeInterface{
//implementation
}
//here is classs, that I need to create. T must extend Fragment and implements
//SomeInterface. But, I'm afraid, if I'll use MyFragment directly, it will create a
//dependence of SomeClass from MyFragment.
public class SomeClass /*generic?*/ {
public SomeClass(T parent);
}
有可能吗?
此外,使用我的 T 类,我想创建视图,使用 T.getActivity() 作为上下文。
【问题讨论】:
-
您能提供更多信息吗?使用 Java 反射,您可以将
Class对象传递给方法,并通过getInterfaces()检查其实现的接口,以及获取/调用Constructor以创建新实例。但是您的具体用例尚不清楚,因此很难弄清楚如何将这些项目组合在一起以解决您的特定问题。 -
@Devunwired,更新了我的问题