为了减少类的依赖性,我想通过向继承某个类和实现接口的构造函数传递参数(使用泛型类)来实现。例如:
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()作为上下文。
Class
对象传递到方法中,并通过getInterfaces()
检查其实现的接口,以及获取/调用Constructor
来创建新实例。但是您的具体用例不清楚,因此很难弄清楚如何将这些项目组合在一起来解决您的特定问题。 - devunwired