繁体   English   中英

隐藏一个枚举常量

[英]hiding an enum constant

我以枚举为例,介绍了如何按顺序遍历树结构(迭代器使用这些枚举常量来决定如何遍历树):

/**
 * The result type of an {@link IVisitor} implementation.
 * 
 * @author Johannes Lichtenberger, University of Konstanz
 */
public enum EVisitResult {
  /** Continue without visiting the siblings of this node. */
  SKIPSIBLINGS,

  /** Continue without visiting the descendants of this node. */
  SKIPSUBTREE,

  /** Continue traversal. */
  CONTINUE,

  /** Terminate traversal. */
  TERMINATE,

  /** Pop from the right sibling stack. */
  SKIPSUBTREEPOPSTACK
}

但是,最后一个枚举常量仅用于内部访问者,不应从使用公共API的用户使用。 有什么想法可以隐藏“ SKIPSUBTREEPOPSTACK”吗?

您所能做的就是证明不应该使用它。

另一种方法是使用接口

public interface EVisitResult {
}

public enum PublicEVisitResult implements EVisitResult {
  /** Continue without visiting the siblings of this node. */
  SKIPSIBLINGS,

  /** Continue without visiting the descendants of this node. */
  SKIPSUBTREE,

  /** Continue traversal. */
  CONTINUE,

  /** Terminate traversal. */
  TERMINATE,
}

enum LocalEVisitResult implements EVisitResult {
  /** Pop from the right sibling stack. */
  SKIPSUBTREEPOPSTACK
}

如果您想同时为公共API和内部实现使用枚举,则可以有2个枚举

private enum InternalFoo
    foo1
    foo2
    foox

private void doFoo(InternalFoo foo)
    switch(foo)
        case foo1
        ...


-----


public enum Foo
    foo1(InternalFoo.foo1)
    foo2(InternalFoo.foo2)
    // no foox

    InternalFoo internal;
    Foo(InternalFoo internal){ this.internal=internal; }

public void doFoo(Foo foo)
    doFoo(foo.internal);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM