内部 Class 在一个实现相同接口的接口中,我们通过这个实现了什么?

Inner Class inside an Interface implementing same Interface, what we are achieving by this?

我的问题:

我在看 TextWatcher 的源代码,但我没有理解这里的概念。 扩展到 NoCopySpan 有什么意义?

TextWatcher.java:

public interface TextWatcher extends NoCopySpan {
     public void beforeTextChanged(CharSequence s, int start, int count, int after);
     public void onTextChanged(CharSequence s, int start, int before, int count);
     public void afterTextChanged(Editable s);
}

NoCopySpan.java:

package android.text;

/**
 * This interface should be added to a span object that should not be copied into a new Spanned when performing a slice or copy operation on the original Spanned it was placed in.
 */
public interface NoCopySpan {
    /**
     * Convenience equivalent for when you would just want a new Object() for
     * a span but want it to be no-copy.  Use this instead.
     */
    public class Concrete implements NoCopySpan {}
}

NoCopySpan 只是 marker interface. According to javadoc it is used to modify behavior of copy routine of Spanned objects (it relies on type of components). For example android.text.SpannableStringBuilder 使用这样的继承信息来跳过构建时的跨度复制。

这种方法有一些缺点,但仍然很常见。 Concrete class 存在的原因是提供方便的方法来构造 NoCopySpan 接口的 on-op 虚拟(或默认)实现。

至于接口NoCopyScan的规范,实现相同接口的内部class可以用作默认implementation/representation外部任何地方的接口。

inner class in an interface 被隐含地视为静态内部 class。我们可以像静态内部class一样实例化接口的内​​部class,然后把NoCopyScan使用起来。请在下面的内部 class 的帮助下找到一个描述接口默认实现用法的简单示例:

/* package whatever; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
interface NoCopy{

public void doStuff();

public class Conc {             //Inner Class
    public int retStuff(){
        return 2;
    }
    }




//  public void doStuff(){
//      System.out.println("Overriding in inner class");
//  }
}


 class ConcOut {

    public int returnStuff(){
        return 5;
    }


    public void doStuff(){
        NoCopy.Conc innerObj = new NoCopy.Conc();    //Instantiating inner class
        //NoCopy.Conc innerObj = (new ConcOut()).new Conc();

        System.out.println("overriding in outside class ConcOut "+ innerObj.retStuff());                              // calling the method of inner class
    }
}

class Ideone
{
    public static void main (String[] args) throws java.lang.Exception
    {
        // your code goes here
        ConcOut conObj = new ConcOut();
        conObj.doStuff();
        //ConcOut.Conc concInner = conObj.new Conc();

    }
}