为了减少类的dependece通用类,我想送参数(使用泛型类)来扩展一些类并实现接口,例如扩展类并实现接口
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 2012-01-15 16:41:29
@Devunwired,更新了我的问题 – 2012-01-15 16:44:23