刚开始在论坛里提到private interface时候,很多人都很迷惑:interface
不是让别人来实现的吗,为什么会有private的interface啊!应该说这不全对,
的确,同class一样,一般的interface只能是public或是friendly的(当然还
可以是class还可以是final的,如果你不想别人改写你的class),但是对于
nesting inteface 是可以被声明为private的:先看看《TIJ》中是一个简单例子:
+--------------------------------------------------------------+
//: c08:NestingInterfaces.java
// From 'Thinking in Java, 2nd ed.' by Bruce Eckel
// www.BruceEckel.com. See copyright notice in CopyRight.txt.
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 {//---private
void f();
}
private class DImp implements D { //was implemented by private class
public void f() {}
}
public class DImp2 implements D {//Notice:was implements by public class
public void f() {}
}
public D getD() { return new DImp2(); }
private D dRef;
public void receiveD(D d) {
dRef = d;
dRef.f();
}
}
interface E {
interface G {
void f();
}
// Redundant "public":
public interface H {
void f();
}
void g();
// Cannot be private within an interface:
//! private interface I {}
}
public class NestingInterfaces {
public class BImp implements A.B {
public void f() {}
}
class CImp implements A.C {
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();
// Can't access A.D:
//because D is private
//! A.D ad = a.getD();
// Doesn't return anything but A.D:
//!A.DImp2 di2 = a.getD();
/**Error message---------------+
---------- Compile ----------
NestingInterfaces.java:83: incompatible types
found : A.D
required: A.DImp2
A.DImp2 di2 = a.getD(); ^
1 error
----------------------------------*/
// Cannot access a member of the interface:
//! a.getD().f();
/**--Error Messae-------------------
---------- Compile ----------
NestingInterfaces.java:85: f() in A.D is not defined
in a public class or interface; cannot be accessed from
outside package
a.getD().f(); ^
1 error
-------------------------------------*/
// Only another A can do anything with getD():
A a2 = new A();
a2.receiveD(a.getD());
}
} ///:~
==================================================================
刚开始我认为privte interface只能被private 实现,但是很明显, public class DImp2
也实现了private interface,不过虽然DImp2是public的,但是他却不能对外界说他实现了一个
private interface,也就是说在inteface D所在class外产生的DImp2的对象不能向上转型,
+--动手实验一下-----------------------+
class PrivateInterface
{
private interface InterfaceTest {//private
void f();
}
public class ImplementsTest implements InterfaceTest
{
public void f(){//override
System.out.println("Test_1");
}
}
public InterfaceTest Getme(){
return new ImplementsTest();
}
public void Test(InterfaceTest ab){
ab.f();
System.out.println(ab);
}
public static void main(String arg[]){
PrivateInterface a = new PrivateInterface();
//PrivateInterface.InterfaceTest pi=a.Getme();//Upcasting:可以向上转型,在同一个class中
pi.f(); //NO problem
a.Test(a.Getme());//NO problem
}
}
class Mytest
{
public static void main(String args[]){
PrivateInterface m= new PrivateInterface();
// m.Test(m.Getme());//可以通过这个来调用f()
/**--------------------Reason-----------------------------+
因为Getme()和Test()都是public的,所以对外是可被
调用的,且Test()方法中的对象拥有对Getme()返回值(x)的
使用权,所以可以通过Test()来调用(x),并通过x来调用
ImplementsTest中的f(),[实际上是:x先向上转型为
InterfaceTest,然后再调用对应的覆写方法,这里因该是
用到了多态里面的后期绑定吧(late-binding)]
+----------------------------------------------------------------*/
//由于InterfaceTest是private的,所以对外不可见。
//更谈不上向上转型了(见下):
PrivateInterface.InterfaceTest pi=m.Getme();//Errior
pi.f();//Error
}
}
---------- Compile ----------
PrivateInterface.java:30: PrivateInterface.InterfaceTest has private
access in PrivateInterface
PrivateInterface.InterfaceTest pi=m.Getme();
^
PrivateInterface.java:31: f() in PrivateInterface.InterfaceTest is not
defined in a public class or interface; cannot be accessed from outside package
pi.f();
^
2 errors
输出完成 (耗时 1 秒) - 正常终止
+---------------------------------------------+
初步认识,可能有不准确或是遗漏的地方,继续认识当中…………