剛開始在論壇裏提到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 秒) - 正常終止
+---------------------------------------------+
初步認識,可能有不準確或是遺漏的地方,繼續認識當中…………