[英]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.