如何从抽象类扩展枚举类? [英] How to extend enum class from abstract class?

查看:598
本文介绍了如何从抽象类扩展枚举类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有这样的事情:

public enum Token
{
     FOO("foo", "f"),
     QUIT("quit", "q"),
     UNKNOWN("", "");
     ...

     public parse(String s) {
         for (Token token : values()) {
              ...
              return token;
         }
         return UNKNOWN;
     }
}

抽象类:

abstract class Base 
{
    private boolean run;

    Base() {
        run = true;

        while (run) {
             inp = getInput();
             act(inp);
        }
    }

    public boolean act(String s) {
        boolean OK = true;
        switch (Token.parse(inp)) { /* Enum */
             case FOO:
                    do_foo();
                    break;
             case QUIT:
                    run = false;
                    break;
             case UNKNOWN:
                    print "Unknown" + inp;
                    OK = false;
                    break;
             }
         }
         return OK;
    }
}

扩展器:

class Major extends Base
{

}

我想要的是扩展行为,如果 super 不处理它然后尝试在 Major 中处理它。例如。添加 PRINT_STAT(print-statistics,ps) - 但同时让 Base 类处理默认值如 QUIT

What I want is to extend act as in if super does not handle it then try to handle it in Major. E.g. add PRINT_STAT("print-statistics", "ps") - but at the same time let the Base class handle defaults like QUIT.

这是一种完全错误的做法吗?

Is this a completely wrong approach?

到目前为止我所做的是添加界面通常:

What I have done so far is add an interface Typically:

public interface BaseFace
{
      public boolean act_other(String inp);
}

并且在类中实现BaseFace

      case UNKNOWN:
          OK = act_other(inp);

并且在课程 Major 中:

  public boolean act_other(String inp) { 
       if (inp.equals("blah")) {
            do_blah();
            return true; 
       }
       return false;
  }

这看起来像是可用的设计吗?

Does this look like a usable design?

主要问题:

是否有一些好方法可以扩展令牌类,以便我可以在 Major 中使用相同的切换方法,如 Base ?我想知道的是,如果有一个是更好的设计,第二,如果我必须为 Major 创建一个新的令牌类,或者如果我以某种方式可以扩展或以其他方式重新使用现有。

Is there some good way to extend the Token class such that I can use the same switch approach in Major as in Base? What I wonder is if there for one is a better design and second if I have to make a new Token class for Major or if I somehow can extend or otherwise re-use the existing.

编辑:概念点是 Base 我可以轻松地在处理各种类型输入的不同项目中重复使用的类。

Point of concept is to have the Base class that I can easily re-use in different projects handling various types of input.

推荐答案

所有枚举隐含扩展枚举。在Java中,一个类最多可以扩展一个其他类。

All enums implicity extend Enum. In Java, a class can extend at most one other class.

但是,您可以让您的枚举类实现接口

You can, however, have your enum class implement an interface.

来自这个关于枚举类型的Java教程


注意:所有枚举都隐式扩展java.lang.Enum。因为类只能扩展一个父类(请参阅声明类),所以Java语言不支持多重继承状态(请参阅状态,实现和类型的多重继承),因此枚举不能扩展其他任何内容。

Note: All enums implicitly extend java.lang.Enum. Because a class can only extend one parent (see Declaring Classes), the Java language does not support multiple inheritance of state (see Multiple Inheritance of State, Implementation, and Type), and therefore an enum cannot extend anything else.

编辑Java 8:

自Java 8起,界面可以包括默认方法。这允许您在接口中包含方法实现(但不包括状态)。虽然此功能的主要目的是允许公共接口的演变,但您可以使用它来继承定义多个枚举类之间的公共行为的自定义方法。

As of Java 8, an interface can include default methods. This allows you to include method implementations (but not state) in interfaces. Although the primary purpose of this capability is to allow evolution of public interfaces, you could use this to inherit a custom method defining a common behavior among multiple enum classes.

然而,这可能很脆弱。如果稍后将具有相同签名的方法添加到 java .lang.Enum 类,它会覆盖你的默认方法。 (当在类的超类和接口中定义方法时,类实现总是获胜。)

However, this could be brittle. If a method with the same signature were later added to the java.lang.Enum class, it would override your default methods . (When a method is defined both in a class's superclass and interfaces, the class implementation always wins.)

例如:

interface IFoo {
    public default String name() {
        return "foo";
    }
}

enum MyEnum implements IFoo {
    A, B, C
}

System.out.println( MyEnum.A.name() );  // Prints "A", not "foo" - superclass Enum wins

这篇关于如何从抽象类扩展枚举类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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