简体   繁体   English

创建匿名类的对象

[英]Create object of anonymous class

We have inner class 我们有内心阶级

class OuterClass
{
  public Iterator getIterator(final String name)
  {
    class LocalIterator implements Iterator
    {
        public Iterator next()
        {
            return new LocalIterator();
        }
    }
    return new LocalIterator();
 }
}

Is it possible to make an anonymous class with all functionality of Local iterator and make getIterator return object of that anonymous class? 是否可以使用Local iterator的所有功能创建一个匿名类,并使g​​etIterator返回该匿名类的对象? The main problem is - what should be instead of 主要问题是 - 应该是什么而不是

  return new LocalIterator();

Im not sure if I understand your question correctly. 我不确定我是否正确理解你的问题。 But if you want to use an anonymous class you can do: 但是如果你想使用匿名类,你可以这样做:

class OuterClass {

    public Iterator<Object> getIterator(final String name) {
        return new Iterator<Object>() {

            @Override
            public boolean hasNext() {
                // validate if there is a next object
                return false;
            }

            @Override
            public Object next() {
                // get the next object and return it, throw an exception if there is no next object
                return null;
            }
        };
    }
}

In general, you are always able to create an instance of any interface using anonymous classes (see, eg, http://docstore.mik.ua/orelly/java-ent/jnut/ch03_12.htm ). 通常,您始终可以使用匿名类创建任何接口的实例(请参阅,例如, http//docstore.mik.ua/orelly/java-ent/jnut/ch03_12.htm )。 You also have access to the name parameter within the class and to the this instance of the outer class using OuterClass.this . 您还可以使用OuterClass.this访问类中的name参数和外部类的this实例。

You should give this design pattern a try: 你应该试试这个设计模式:

class OuterClass implements Iterable {
    ....

    public Iterator iterator() {
        return new Itr();
    } 

    private class Itr implements Iterator {
        ....
    }
}
  1. it doesn't expose iterator implementation 它不公开迭代器实现
  2. it is easy to read and maintain this code 它易于阅读和维护此代码
  3. Itr instances can be used everywhere as a common Iterator Itr实例可以在任何地方用作常见的Iterator

Make sure OuterClass implements Iterable in order to be usable with the foreach loop. 确保OuterClass实现Iterable以便与foreach循环一起使用。 Then in iterator() you could return an instance of an anonymous iterator (replace T with the concrete type you need). 然后在iterator()您可以返回一个匿名迭代器的实例(用您需要的具体类型替换T )。

public class OuterClass<T> implements Iterable<T> {

    @Override
    public Iterator<T> iterator() {
        return new Iterator<T>() {

            @Override
            public boolean hasNext() {
                // TODO Auto-generated method stub
                return false;
            }

            @Override
            public T next() {
                // TODO Auto-generated method stub
                return null;
            }
        };
    }
}

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

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