1.撰写一个interface,令它具备至少一个函数,并在其中某个函数内定义一个inner class用以实现该interface.这个函数必须传回一个reference指向你的哪个interface.
2.撰写一个"实现出public interface"的private inner class.为它撰写函数,使后者传回一个reference,指向该private innver class的一个实体(instance),并将它向上转型至该interface.

解决方案 »

  1.   

    翻译得有出入,原题是:13. Create an interface with at least one method, and implement that interface by defining an inner class within a method, which returns a reference to your interface.17. Create a private inner class that implements a public interface. Write a method that returns a reference to an instance of the private inner class, upcast to the interface. Show that the inner class is completely hidden by trying to downcast to it.给楼主我的答案(不是标准答案,引供参考)-------------------------------------------------------第13题://I.java
    public interface I {
        void f();
    } //:~//ReturningI.java
    public class ReturningI {
        public I g() {
            return new I() {  // 匿名内部类
                public void f() { }  // 必须实现I中的f()方法
            };
        }
    } //:~------------------------------------------------------第17题:// A.java
    public class A {
        private class Inner implements I {  // 私有内部类Inner实现接口I
            public void f() { }
        }
        public I a() {
            return new Inner();  // 将Inner的实例向上转型为I,并返回
        }
    } //:~// B.java
    public class B {
        public void b() {        I i = new A().a();  //I是public的,没问题        // 试图将i向上转型为A.Inner,但Inner是A私有的,因此编译错误
            // A.Inner ai = (A.Inner) i;     }
    } //:~