简体   繁体   English

class中的接口方法没有实现吗?

[英]Interface methods in a class that does not implement it?

public interface Iterator<T> {

    // Returns true if the iterator is valid (points to an element), false otherwise.
    boolean isValid();

    // Returns the current element and moves forward. This method can only be called if the iterator is valid. If the iterator points to the last element, it becomes invalid after the call.
    T next();

    // Returns the current element and moves backwards. This method can only be called if the iterator is valid. If the iterator points to the first element, it becomes invalid after the call.
    T prev();
}

In a class that does not implement interface Iterator, how is it possible to create a method that returns Iterator<K> , when you can only create methods for an interface inside a class that implements it?在不实现接口 Iterator 的 class 中,当您只能为实现它的 class 中的接口创建方法时,如何创建返回Iterator<K>的方法?

public class ABC<K> implements EFG<K>{
public Iterator<K> minIt() {
   //method body
   //return Iterator<K> variable 
}
}

The class ABC containing the method minIt() does not implement Iterator<T> class 包含方法minIt()ABC未实现Iterator<T>

(No classes implement the interface Iterator <T> ) (没有类实现接口Iterator <T>

Simple.简单的。 By making a class that implements it.通过制作一个 class 来实现它。 Note that you have a type that you came up with on your own and you named it Iterator .请注意,您有一个自己想出的类型,并将其命名为Iterator Given that java.util.Iterator exists, this is a really bad idea.鉴于java.util.Iterator存在,这是一个非常糟糕的主意。 You should pick another name.你应该选择另一个名字。

public class ABC<K> implements EFG<K> {
  // Let's say this contains the items that can be iterated over.
  private List<K> list = new ArrayList<K>();

  class MyIterator implements my.pkg.Iterator<K> {
    private int position = 0;
    @Override public boolean isValid() {
      return position > -1 && position < list.size();
    }

    @Override public K next() {
      if (!isValid()) throw new NoSuchElementException();
      return list.get(position++);
    }

    @Override public K prev() {
      if (!isValid()) throw new NoSuchElementException();
      return list.get(position--);
    }
  }

  public Iterator<K> minIt() {
    return new MyIterator<K>();
  }
}

Note that classes that you put in classes can only be constructed in instance contexts within that class: They have a 'secret' field of your outer's type.请注意,您放入类中的类只能在该 class 内的实例上下文中构造:它们具有外部类型的“秘密”字段。 Hence why the code in MyIterator can access the list field of your outer class.因此,为什么 MyIterator 中的代码可以访问外部 class 的list字段。

Java has 'anonymous inner class literal' syntax which lets you shorten this: Instead of explicitly declaring class MyIterator , you can also write: Java 具有“匿名内部 class 字面量”语法,可让您缩短此句法:除了显式声明class MyIterator ,您还可以编写:

public Iterator<K> minIt() {
  return new your.pkg.Iterator<K>() {
    private int position = 0;
    @Override public boolean isValid() {
      // same code goes here as the previous snippet
    }
  };
}

This anonymous inner class form is a lot more common.这种匿名内部 class 形式更为常见。 It's just syntax sugar - a shorter way to write the same thing.它只是语法糖——一种编写相同内容的更短方式。

You can use an Anonymous Class that implements the interface:您可以使用实现接口的匿名 Class

For instance:例如:

interface Foo<T> {
    T foo();
}
class Bar<T>  {
   T t;
   public Foo<T> bar() {
       return new Foo<T>() { // <-- Anonymous class implementing `Foo`
            public T foo() {
                return t;
            }
       };
   }
}

Execution:执行:

Bar<String> b = new Bar<>();
b.t = "hello"; // with a setter in real life
Foo<String> f = b.bar();
f.foo(); // will return "hello"

The other option which I think would be the most common is to use a method that returns the interface, for instance the list interface has an iterator() method even though it itself doesn't implements the Iterator interface.我认为最常见的另一个选项是使用返回接口的方法,例如列表接口有一个iterator()方法,即使它本身没有实现Iterator接口。

List<String> list = new ArrayList<>();
Iterator<String> stringIterator = list.iterator();

Here's the implementation 这是实现

暂无
暂无

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

相关问题 无法从实现接口的类中实现方法? - Unable to implement methods from a class that implements an interface? 在jar中实现接口但不覆盖方法的类 - Class that implements an interface in a jar, but does not override the methods 类 SpringHibernateJpaPersistenceProvider 没有实现请求的接口 PersistenceProvider - Class SpringHibernateJpaPersistenceProvider does not implement the requested interface PersistenceProvider 为什么ZonedDateTime类没有实现TemporalAdjuster接口 - why ZonedDateTime class does not implement TemporalAdjuster interface 是不是必须强制在Child抽象类中实现所有接口方法,而必须强制在Grand Child类中实现所有接口方法? - Y it's not mendatory to implement all interface methods in Child abstract class But Mendatory to implement all Interface methods in Grand Child class? 为什么超类实现了应该由子进程实现的接口方法? - Why does a superclass implement interface methods which are supposed to be implemented by the child? Map 接口是否扩展或实现了一些其他接口或 class? - Does Map interface extend or implement some other interface or class? 将最终类强制转换为该类未声明要实现的兼容接口 - Cast a final class to a compatible interface that the class does not claim to implement 实现接口类的类下的代码不执行 - the code under class which implement the interface class does not execute 接口类型变量可以从没有实现它的 class 中的该接口调用方法吗? - Can interface type variable call methods from that interface in a class that doesn’t implement it?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM