您是否可以创建一个包含java内置类的所有函数的接口?
我想做什么:
我有很多扩展Fragment的类,在类中有一些属性(get,set)和一些函数。在创建了不同类的对象之后,我将它们放在一个通用的片段列表中。
但我想引入多态:1接口类和所有其他类实现它,以便它们可以覆盖函数。
但问题是,如果我想使用多态,我需要一个接口,接口应该是一个片段,所以我仍然可以把这些片段放到一个列表中,所以我可以从Fragment类创建一个接口或者可以接口是从片段继承的吗?
在图像上,您可以看到当前的情况
以下是一些显示我想要实现的内容的代码:
所以我的主要功能是xmlSAXParser,当我遇到一个标签时,我创建了一个正确类的对象:
case template:
switch (typeObjecten.toType(atType)) {
case NOVALUE:
break;
case chapter:
chapter = new ChapterFragment();
typeObject = "chapter";
break;
case execute:
execute = new ExecuteFragment();
typeObject = "execute";
break;
case first:
first = new FirstFragment();
typeObject = "first";
break;
...
当我找到结束标记时,我将正确的对象添加到列表中:
case template:
switch (typeObjecten.toType(typeObject)) {
case NOVALUE:
break;
case chapter:
fragments.add(ownFrag);
break;
case execute:
fragments.add(execute);
break;
case first:
fragments.add(first);
break;
case navigation:
fragments.add(navigation);
break;
case story:
fragments.add(story);
break;
....
也有可能在每个班级中背景都可以改变,现在我必须检查对象是否正在调用正确的函数:
case background:
if (typeObject.equalsIgnoreCase("navigation")) {
navigation.setBackgroundColor("#" + text);
} else if (typeObject.equalsIgnoreCase("chapter")) {
//chapter.setBackgroundColor("#" + text);
} else if (typeObject.equalsIgnoreCase("first")) {
ownFrag.setBackgroundColor("#" + text);
first.setBackgroundColor("#" + text);
} else if (typeObject.equalsIgnoreCase("execute")) {
execute.setBackgroundColor("#" + text);
} else if (typeObject.equalsIgnoreCase("story")) {
story.setBackgroundColor("#" + text);
} else if (typeObject.equalsIgnoreCase("intro")) {
intro.setBackgroundColor("#" + text);
} else if (typeObject.equalsIgnoreCase("mission")) {
mission.setBackgroundColor("#" + text);
} else if (typeObject.equalsIgnoreCase("question")) {
question.setBackgroundColor("#" + text);
} else if (typeObject.equalsIgnoreCase("info")) {
info.setBackgroundColor("#" + text);
}
break;
...
如果我可以创造一个全球性的,那就太好了。片段,然后程序发现自己从它必须调用的类中发挥作用(多态?)
答案 0 :(得分:1)
另一种选择是将Fragment抽象化并让你的类扩展它。然后,如果可能的话,您还可以添加一些共享功能。
基于你评论Fragment是一个内置的类而不是可编辑的,似乎有几个选项。你可以参考一般扩展片段的对象<?扩展片段>或者通过在Fragment上创建一个包装类来保存和维护它的实例。这会让你有更多的控制权。
一个简单的例子可能如下所示。
class CannotModify{
public void doSomething(){}
}
public interface AddThisIfYouWant{
public void doSomething();
}
public class Wrap extends CannotModify, implements AddThisIfYouWant{
private final CannotModify yourInstance;
public Wrap(){
this.yourInstance=new CannotModify();
}
@Override
public void doSomething(){
super.doSomething();
}
}
如果您只需要在片段级别引用对象,也可以扩展Fragment。例如:
public class AnotherFragment extends Fragment{...}
...
public void somethingWithFragments(List<? extends Fragment> fragments){
for(Fragment fragment:fragments){
...
}
}
...
答案 1 :(得分:1)
在Fragment是内置类的信息之后编辑。
您的基本问题的答案是:不,在Java中,您无法使用新行为扩展预定义的类(例如,使其实现另一个接口)。例如,您可以在Objective-C中执行此操作。但在你的情况下看起来并不像你需要那样。
定义Fragment的子类,它具有您需要的行为,并实现您需要它实现的接口,并从该子类派生您的其他类。
旧回答:
你已经自己给出了答案:
但问题是,如果我想使用多态,我需要一个 界面,界面应该是片段
由于您现在已经绘制了类图,因此Fragment类没有数据也没有默认行为,因此根本没有理由使它成为所有其他类的基类。只需将Fragment定义为一个接口(使用您需要的方法声明),并为其实现该接口的其他类指定。
答案 2 :(得分:0)
您应该为Fragment创建一个接口,并使所有类扩展该接口
答案 3 :(得分:0)
首先。没有接口不能扩展类,这会破坏接口的语义(只有签名)。
你可以做的是让“Fragment”简单地实现新创建的接口。
答案 4 :(得分:0)
您可以制作一般FragmentInterface
和其他扩展FragmentInterface
的接口。
为什么在创建基本接口时只需要1个接口,以及扩展该基接口的其他接口?
顺便说一下,一个班级也可以实现多个interfaces
。