package lu.nesting;
import static lu.utils.Print.*;
import lu.nesting.A.DImp2;
/*
* 接口可以被实现为private,(相同的语法既适用于嵌套接口也适用于嵌套类)
* 好处:A.DImp2只能被其自身使用。你无法说他实现了一个private接口D,实现private接口只是一种方式,
* 他可以强制该接口中的方法定义不要添加任何类型信息(不允许向上转型)*/
class A{
interface B{
void f();
}
public class BImp implements B{
public void f(){}
}
private class BImp2 implements B{
public void f(){}
}
public interface C{
void f();
}
class CImp implements C{
public void f(){}
}
private class CImp2 implements C{
public void f(){}
}
private interface D{
void f();
}
private class DImp implements D{
public void f(){}
}
public class DImp2 implements D{
public void f(){print("你是猪么?");}
}
public D getD(){return new DImp2();}
private D dRef;
public void receiveD(D d){//将返回值交给有权使用它的对象。
dRef=d;
dRef.f();
}
}
interface E{//接口之间可以嵌套。然而作用于接口的各种规则特别是所有接口元素都必须是public的,
interface G{//因此嵌套在另一个接口中的接口自动就是public的,而不能声明为private的。
void f();
}
public interface H{
void f();
}
void g();
//The interface member type I can only be public
//Cannot be private within an interface;
//private interface I{}
}
public class NestingInterface{//注意 当实现某个接口时并不需要实现嵌套在其内部的任何接口。而且private接口不能在定义它的类之外被实现。
public class BImp implements A.B{
public void f(){}
}
class CImp implements A.B{
public void f(){}
}
//Cannot implement a private interface except
//within that interface's defining class
//!class DImp implements A.D{
//!public void f(){}
//!}
class EImp implements E{
public void g(){}
}
class EGImp implements E.G{
public void f(){}
}
class EImp2 implements E{
public void g(){}
class EG implements E.G{
public void f(){}
}
}
public static void main(String[] args)
{
A a=new A();
//cannot 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());
}
}