如何在Java中编写一个传递类名的函数?

时间:2015-11-20 09:52:27

标签: java function

我有下面的代码。

    Fragment emptyViewFragment;
    Fragment songListingFragment;

    // .... and various codes ...

    if (emptyViewFragment == null) {
        emptyViewFragment =
                (EmptyViewFragment) getFragmentManager().findFragmentByTag(EmptyViewFragment.TAG);
        if (emptyViewFragment == null) {
            emptyViewFragment = new EmptyViewFragment();
        }
    }
    addFragment(emptyViewFragment, EmptyViewFragment.TAG);

    if (songListingFragment == null) {
        songListingFragment =
                (SongListingFragment) getFragmentManager().findFragmentByTag(SongListingFragment.TAG);
        if (songListingFragment == null) {
            songListingFragment = new SongListingFragment();
        }
    }
    addFragment(songListingFragment, SongListingFragment.TAG);

这两个if-else代码看起来非常相似,我正在考虑从中创建一个函数,以便我可以像下面那样编写代码: -

    EmptyViewFragment emptyViewFragment;
    SongListingFragment songListingFragment;

    // .... and various codes ...

    createFragment(emptyViewFragment, EmptyViewFragment);
    createFragment(songListingFragment, SongListingFragment);

甚至更好(因为第二个参数是对象'类型中的第一个参数传递)。

    EmptyViewFragment emptyViewFragment;
    SongListingFragment songListingFragment;

    // .... and various codes ...

    createFragment(emptyViewFragment);
    createFragment(songListingFragment);

我的createFragment函数应如何编写?

4 个答案:

答案 0 :(得分:3)

也许是这样的

private void a (Fragment frag, Class<? extends Fragment> clazz, String tag) {
    if (frag == null) {
        frag = getFragmentManager().findFragmentByTag(tag);
        if (frag == null) {
            try {
                frag = clazz.newInstance();
            } catch (InstantiationException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
    }
    addFragment(frag);
}

你可以访问里面的tag属性并删除tag参数

<强>更新

我现在无法测试,但我认为您可以通过这种方式访问​​静态成员TAG

private void a (Fragment frag, Class<? extends Fragment> clazz) {

    try {
        if (frag == null) {

            frag = getFragmentManager().findFragmentByTag(clazz.getField("TAG").get(null).toString());

            if (frag == null) {
                frag = clazz.newInstance();
            }
        }
    } catch (InstantiationException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    } catch (NoSuchFieldException e) {
        e.printStackTrace();
    }
    addFragment(frag);
}

答案 1 :(得分:2)

您可以使用抽象工厂代替使用该类:

Fragment createFragment(Fragment fragment, FragmentFactory factory) {
    if (fragment == null) {
        fragment = getFragmentManager().findFragmentByTag(factory.getTag());
        if(fragment == null) {
            fragment = factory.createFragment();
        }
    }
    addFragment(fragment, factory.getTag());
}

其中FragmentFactory是一个接口,每个片段类型都有一个实现。

答案 2 :(得分:1)

尝试这样的事情:

public addFragment(Fragment frag) throws IllegalAccessException, InstantiationException {
    Fragment newFrag = frag.getClass().newInstance();
    //...
}

如果frag == null,这将不起作用。

如果这是您的使用案例,请使用Class<Fragment> clz = EmptyViewFragment.class获取课程,然后致电clz.newInstance()

答案 3 :(得分:0)

/**
 * @param fragment
 * @throws IllegalAccessException 
 * @throws InstantiationException 
 */
private static void createFragment(Fragment fragment, String tag, Class<? extends Fragment> clazz) throws InstantiationException, IllegalAccessException {
    if (fragment == null) {
        fragment =
                 getFragmentManager().findFragmentByTag(tag);
        if (fragment == null) {
            fragment = clazz.newInstance();
        }
    }
    addFragment(fragment, tag);
}

假设Fragment是其他* Fragment类的Parent类,并且具有以下方法定义:

    /**
 * @return
 */
private static Fragment getFragmentManager() {
    // TODO Auto-generated method stub
    return null;
}
相关问题