【问题标题】:getPartActionList method should be static or Non-Static in java?java中的getPartActionList方法应该是静态的还是非静态的?
【发布时间】:2021-04-05 02:19:02
【问题描述】:

这是我的代码,我想使用多态但是 getPartActionList 是静态的,我不使用多态性。 此方法返回参数名称 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;

【问题讨论】:

  • 请展示一些使用 PartAction 类的示例代码,并解释您期望从中得到什么输出。
  • 我上传了一张照片。
  • 请编辑您的问题并将您的代码放入其中。截图太难阅读,不允许我们编译代码。
  • 我在上面不同的地方使用了这段代码。

标签: java methods static polymorphism


【解决方案1】:

您似乎总是在已经有操作的上下文中使用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 语句的该分支处理的 Action 类型发生变化,您需要手动更改的事情就会减少。

【讨论】:

  • 我的问题是这个方法必须是静态的,因为它在样本之间是固定的,我不能使用多态性。你说这个方法不是静态的?
  • 为什么不能使用多态性?从上面的示例看起来,当您获得操作列表时,您总是有一个可用的Action
  • 因为这个方法是静态的。抽象静态方法();是编译器错误。
  • 是的,不要让它成为静态的。
  • 这是标准吗?因为这个方法是独立于实例的。
猜你喜欢
  • 2012-06-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-05-24
  • 1970-01-01
  • 2011-02-01
  • 2017-04-30
  • 1970-01-01
相关资源
最近更新 更多