如何确定哪些孩子称为父类的方法 [英] How to determine which child has called a parent’s method

查看:108
本文介绍了如何确定哪些孩子称为父类的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是相同的的TabBar在多个活动。而且,由于有涉及的 onOptionsItemSelected 广泛的逻辑,我想一旦写入相关的方法,然后重新使用它们。因此,我决定创建一个超类名为 CustomActionBarActivity ,然后让孩子们活动的扩展。一个特别的问题,我需要帮助是我怎么能知道哪些孩子造成了 onOptionsItemSelected 被称为?为了阐明,我将present在解决方案的总体code,然后一个失败的尝试。

I am using the same tabBar across multiple Activities. And since there are extensive logic involved for onOptionsItemSelected, I want to write the relevant methods once and then reuse them. Hence I am deciding to created a super class called CustomActionBarActivity and then have the children activities extend it. One particular problem I need help with is how can I tell which child has caused the onOptionsItemSelected to be called? To elucidate, I will present the general code and then a failed attempt at a solution.

这是一般的code

public class CustomActionBarActivity extends FragmentActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle item selection
        switch (item.getItemId()) {
            case R.id.tab_dog:
                startActivity(new Intent(this, DogActivity.class));
                return true;
            case R.id.tab_cat:
                startActivity(new Intent(this, CatActivity.class));
                return true;
            case R.id.tab_mouse:
                startActivity(new Intent(this, MouseActivity.class));
                return true;
            case R.id.tab_goose:
                startActivity(new Intent(this, GooseActivity.class));
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

}

失败的尝试

如果我尝试,例如,

case R.id.tab_dog:
  if(!(this instanceof DogActivity))
    startActivity(new Intent(this, DogActivity.class));

然后我得到一个编译错误,使得CustomActionBarActivity不与DogActivity兼容。感谢您的帮助,您可以提供。

then I get a compile error such that CustomActionBarActivity is not compatible with DogActivity. Thanks for any help you can provide.

推荐答案

而不是有你的父类检查使用反射(这是pretty的脆弱的孩子,因为它不与孩子的数量扩展子类创建),也许你可以利用动态分配的,而不是。

Instead of having your parent class inspect the children using reflection (which is pretty fragile since it doesn't scale with the number of children subclasses you create), maybe you could take advantage of dynamic dispatch instead.

例如,也许你可以在你的父活动申报摘要的方法,如:

For example, maybe you could declare an abstract method in your parent activity like:

protected abstract void onTabItemSelected(MenuItem item);

那么你的孩子的活动可以覆盖此方法依赖于所期望的行为。例如, DogActivity 有可能实现这样的:

Then your children activities can override this method depending on the desired behavior. For example, DogActivity might implement it like this:

protected boolean onTabItemSelected(MenuItem item) {
    if (item.getItemId() != R.id.dog_tab) {
        startActivity(new Intent(this, DogActivity.class));
        return true;
    }
    return false;
}

onOptionsItemSelected 方法,然后将这样实现的:

The onOptionsItemSelected method would then be implemented like this:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    if (onTabItemSelected(item)) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

让我知道如果我误解了这个问题。无论哪种方式,你也许可以修改此方法,以满足你的使用情况。

Let me know if I misunderstood the question. Either way, you might be able to modify this approach to suit your use case.

这篇关于如何确定哪些孩子称为父类的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆