如何从父类中调用孩子的方法? [英] How do I call a child's method from the parent class?
问题描述
假设我有一个 ChildFragment 类,它是从 ParentFragment 继承的.目的是让几个带有按钮的活动"和片段"通常由一个 ParentFragment 处理.
Let's say I have a class, ChildFragment that inherits from a ParentFragment. The purpose is to have several Activities and Fragments with buttons that are commonly used handled by one ParentFragment.
当用户按下刷新按钮时,它将在ParentFragment
中调用refresh()
函数.但是,实际上下载此文件的操作由ChildFragment处理,因为每个片段都有不同的内容.因此,只要refresh()
完成,我希望此ParentFragment在子级中调用某些东西.
When the user presses a refresh button, it will call a refresh()
function in the ParentFragment
. However, the action of actually downloading this file is handled by the ChildFragment, because every fragment has different content. So I want this ParentFragment to call something in the child, whenever the refresh()
is done.
我该怎么做?或者,如果有更好的方法,这样做也有帮助.
How can I do this? Or if there is a better way of doing it, it would help too.
推荐答案
您需要在子片段中覆盖refresh
方法.
You need to override the refresh
method in child fragments.
public class PFragment extends Fragment{
protected void refresh(){
//Common stuff here
}
}
在子类ovveride中的刷新方法
In child class ovveride the refresh method
public class CFragment extends PFragment{
public void onClick(){
super.refresh();
}
@Override
protected void refresh(){
//Custom stuff here
}
}
这篇关于如何从父类中调用孩子的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!