它叫什么以及何时在java中用于接口

时间:2013-09-16 07:34:48

标签: java spring interface

在spring框架 spring modules validation jar文件中我看到了以下代码

package org.springmodules.validation.valang.predicates;


    public interface Operator
    {
        public static interface IsNotLowerCaseOperator
            extends Operator
        {
        }

        public static interface IsLowerCaseOperator
            extends Operator
        {
        }

        public static interface IsNotUpperCaseOperator
            extends Operator
        {
        }
        }



public interface OperatorConstants
{
public static final Operator IS_NOT_LOWER_CASE_OPERATOR = new Operator.IsNotLowerCaseOperator() {

    }
;
   public static final Operator IS_LOWER_CASE_OPERATOR = new Operator.IsLowerCaseOperator() {

    }
;
    public static final Operator IS_NOT_UPPER_CASE_OPERATOR = new Operator.IsNotUpperCaseOperator() {

    }
;
public abstract class AbstractPropertyPredicate
    implements Predicate
{
public AbstractPropertyPredicate( Operator operator)
    {  
        setOperator(operator);
    }

 public final Operator getOperator()
    {
        return operator;
    }
     private Operator operator;
     }

使用以下访问时

 if(getOperator() instanceof Operator.IsNotLowerCaseOperator)

我从未见过这种编码风格?它叫什么?何时使用?有什么帮助吗?

1 个答案:

答案 0 :(得分:1)

它被称为匿名内部类,它们的格式为

Instance i = new Instance() {
... extra methods, etc.... possibly implementing abstract methods as inherited from Instance
}

它们用于特定接口或抽象类或“普通”类的简单一次性实现。

大部分时间用于摇摆中的事件处理程序,如

new ActionListener() {
    public actionPerformed(ActionEvent e) {...}
    }
}

在这种情况下,它用于模拟枚举,而在早期Java版本中没有枚举