getpartactionlist方法在java中应该是静态的还是非静态的?

ghhkc1vu  于 2021-06-29  发布在  Java
关注(0)|答案(1)|浏览(318)



这是我的代码,我想使用多态性,但getpartactionlist是静态的,我不使用多态性。此方法返回parametrs partaction类的名称,对所有对象的行为都相同。getpartactionlist方法在我的代码中应该是静态的还是非静态的?请帮帮我。抱歉写得不好。

public class PartAction extends BaseAction {

    private String address;
    private String brand;
    private String part_human;

    public static List<String> getPartActionList() {

        List<String> list = new ArrayList<>();
        list.add("addressName");
        list.add("brandName");
        list.add("partHumanName");
        return list;

    }
switch (categoryEnums) {

        case MAIN_CATEGORY:
            recyclerViewCategory.setAdapter(new CategoryAdapter(getContext(),getMainCategory(), statusFragment, childFragmentManager));
            break;

        case BUY_CATEGORY:
            recyclerViewCategory.setAdapter(new SubcategoryAdapter(getContext(), BuyAction.getBuyActionList(), statusFragment, childFragmentManager,action));
            break;

        case HUMAN_CATEGORY:
            recyclerViewCategory.setAdapter(new SubcategoryAdapter(getContext(), HumanAction.getHumanActionList(), statusFragment, childFragmentManager,action));
            break;

        case HUMAN_SUBCATEGORY_1:
            recyclerViewCategory.setAdapter(new SubcategoryAdapter(getContext(), HumanAction.getSubCategoryHumanList(), statusFragment, childFragmentManager,action));
            break;

        case HUMAN_SUBCATEGORY_2:
            recyclerViewCategory.setAdapter(new SubcategoryAdapter(getContext(), HumanAction.getSecondSubCategoryHumanList(), statusFragment, childFragmentManager,action));
            break;

        case SERVICE_CATEGORY:
            recyclerViewCategory.setAdapter(new SubcategoryAdapter(getContext(), ServiceAction.getServiceActionList(), statusFragment, childFragmentManager,action));
            break;

        case TRANSPORT_CATEGORY:
            FragmentTransaction fragmentTransaction = childFragmentManager.beginTransaction();
            fragmentTransaction.add(R.id.framLayout_child_category, new HomeFragment("حمل و نقل", action));
            fragmentTransaction.addToBackStack(null).commit();
            break;

        case REPAIRS_CATEGORY:
            recyclerViewCategory.setAdapter(new SubcategoryAdapter(getContext(), RepairAction.getRepairActionList(), statusFragment, childFragmentManager,action));
            break;

        case PARTS_CATEGORY:
            recyclerViewCategory.setAdapter(new SubcategoryAdapter(getContext(), PartAction.getPartActionList(), statusFragment, childFragmentManager,action));
            break;
wljmcqd8

wljmcqd81#

你好像总是用 getPartActionList 方法在已具有操作的上下文中:

recyclerViewCategory.setAdapter(new SubcategoryAdapter(getContext(), PartAction.getPartActionList(), statusFragment, childFragmentManager,action));

这意味着您需要确保所使用的类( PartAction 在本例中)匹配 action .
如果你做了一个 getActionList 方法并将其放在 BaseAction 类作为抽象方法,如下所示:

abstract class BaseAction {
   public abstract List<String> getActionList();
}

然后你可以做:

recyclerViewCategory.setAdapter(new SubcategoryAdapter(getContext(), action.getActionList(), statusFragment, childFragmentManager, action));

如果switch语句的分支所处理的操作类型发生更改,那么您就少了一件需要手动更改的事情。

相关问题