classSplitterextendsProcessor{ String process(Object input){ // The split() argument divides a String into pieces: return Arrays.toString(((String)input).split(" ")); } }
publicclassApply{ publicstaticvoidprocess(Processor p, Object s){ System.out.println("Using Processor " + p.name()); System.out.println(p.process(s)); } publicstatic String s = "Disagreement with beliefs is by definition incorrect"; publicstaticvoidmain(String[] args){ process(new Upcase(), s); process(new Downcase(), s); process(new Splitter(), s); } }
// Using Processor Upcase // DISAGREEMENT WITH BELIEFS IS BY DEFINITION INCORRECT // Using Processor Downcase // disagreement with beliefs is by definition incorrect // Using Processor Splitter // [Disagreement, with, beliefs, is, by, definition, incorrect]
// 虽然它这种 abstract 类中直接写具体调用的做法我总感觉很飘逸,但是理解不难 publicabstractclassStringProcessorimplementsProcessor{ public String name(){ return getClass().getSimpleName(); } publicabstract String process(Object input); publicstatic String s = "If she weighs the same as a duck, she’s made of wood"; publicstaticvoidmain(String[] args){ Apply.process(new Upcase(), s); Apply.process(new Downcase(), s); Apply.process(new Splitter(), s); } } classUpcaseextendsStringProcessor{ public String process(Object input){ // Covariant return return ((String)input).toUpperCase(); } } classDowncaseextendsStringProcessor{ public String process(Object input){ return ((String)input).toLowerCase(); } } classSplitterextendsStringProcessor{ public String process(Object input){ return Arrays.toString(((String)input).split(" ")); } }
// Using Processor Upcase // IF SHE WEIGHS THE SAME AS A DUCK, SHE’S MADE OF WOOD // Using Processor Downcase // if she weighs the same as a duck, she’s made of wood // Using Processor Splitter // [If, she, weighs, the, same, as, a, duck,, she’s, made, of, wood]
// Cannot implement a private interface except // within that interface’s defining class: // ! class DImp implements A.D { // ! public void f() {} // ! } classEImpimplementsE{ publicvoidg(){ } }
classEGImpimplementsE.G{ publicvoidf(){ } }
classEImp2implementsE{ publicvoidg(){ }
classEGimplementsE.G{ publicvoidf(){ } } }
publicstaticvoidmain(String[] args){ A a = new A(); // Can’t access A.D: // ! A.D ad = a.getD(); // Doesn’t return anything but A.D: // ! A.DImp2 di2 = a.getD(); // Cannot access a member of the interface: // ! a.getD().f(); // Only another A can do anything with getD(): A a2 = new A(); a2.receiveD(a.getD()); } }
but A.DImp2 shows that it can also be implemented as a public class. However, A.DImp2 can only be used as itself. You are not allowed to mention the fact that it implements the private interface D, so implementing a private interface is a way to force the definition of the methods in that interface without adding any type information (that is, without allowing any upcasting).
个人理解为 private 接口将接口的实现和定义限制在了定义类里面。而且一般使用的时候都是会返回接口类,像上面的 D getD(), 而 D 又是 private 的,限制了他的使用,这应该就是原文中 'A.DImp2 can only be used as itself' 的意思吧
getD() 方法是 private 修饰的嵌套接口的更特殊的使用方式,在 main() 中,我们 comment 了很多对 D 接口的引用,但是这些用法都有编译错误。唯一的使用方式是新建一个 A 对象,调用以 D 为参数的方法。
Interface E 的例子想要说明的事,接口内部也能声明接口,但是秉承接口内部元素必须都是规则,内嵌的接口也必须只能是 public 的