【JAVA學習路-think in java】p185:嵌套接口詳解

總結:

  • 1、class允許有private、protected、default、public接口,但interface中的接口只能是public的(隱public,不必聲明);
  • 2、class中的private接口,不能被繼承。所以private接口只能在該class中被實現。
  • 3、接口中的函數只能是public、static、abstract、default的,不能是private的;
  • 4、在任何情況下,類在繼承接口時,實施接口內的函數時,必須顯式聲明爲public
  • 5、繼承接口時,接口中聲明的函數必須全部給出具體實現(寫出函數體),但該接口中的接口所聲明的函數,可以不必給出具體實現。

 

  • package pkg;
    
    import pkg.Aa.Dimple2;
    
    class Aa{
    	
    	//** interface
    	protected interface B{void f();}//PROTECTED interface is allowed in Class
    	interface C{void f();}//default interface is allowed in Class
    	private interface D{void f();}//PRIVATE interface is allowed in Class
    	
    	interface E{
    		interface G{void f();}//The interface member type G can ONLY be PUBLIC
    		interface H{void f();}
    		void g();//only public, abstract, default, static are permitted
    		void g2();
    		static void g3() {System.out.println("Function body is needed here for static method");};
    	}
    
    	//** class
    	class generalClass{//definite a class in a class
    		void f() {System.out.println("f() in generalClass of class Aa");}
    		private void f2() {System.out.println("f2() in generalClass of class Aa");}
    	}
    	class Bimple implements B{
    		 public void f() {System.out.println("f() with public in Bimple");}
    		 //Cannot reduce the visibility of the inherited method from A.B:add public
    	}
    	class Bimple2 implements B{
    		public void f() {System.out.println("f() in Bimple 2 of class Aa");}
    	}
    	class Cimple implements C{
    		public void f() {System.out.println("f() in Cimple of class Aa");}
    	}
    	private class Dimple implements D{
    		public void f() {System.out.println("f() in Dimple of class Aa");}
    	}
    	public class Dimple2 implements D{
    		public void f() {System.out.println("f() in Dimple2 of class Aa");}
    	}
    	
    	//** method
    	public D dRef; // a interface can be defined!
    	public D getD() {//up cast
    		return new Dimple();
    	}
    	
    	public void receiveD(D d) {// interface can be one argument
    		dRef=d;
    		dRef.f();
    	}
    }
    
    
    //  ...  main  ...
    public class p185{
    	
    	//...>>>implements
    	public class Bimple implements Aa.B{//how to implements a interface in one class
    		@Override
    		public void f() {System.out.println("f() in Bimple  of p185 with interface Aa.B");}
    	}
    	public class Cimple implements Aa.C{
    		@Override
    		public void f(){System.out.println("f() in Cimple of p185 with interface Aa.C");}
    	}
    	
    //	public class Dipmle implements Aa.D{//The type Aa.D is not visible
    //		@Override
    //		public void f(){System.out.println("f() in Cimple of class Bimple of p185");}
    //	}
    	public class Eimple implements Aa.E{
    		//The type p185.Eimple must implement the inherited abstract method Aa.E.g() & Aa.E.g2()
    		public void g() {System.out.println("g() in Eimple of p185 with interface Aa.E");}
    		public void g2() {System.out.println("g2() in Eimple of p185 with interface Aa.E");}
    		//Cannot reduce the visibility of the inherited method from Aa.E
    	}
    	public class EGimple implements Aa.E.G{
    		//The type p185.EGimple must implement the inherited abstract method Aa.E.G.f()
    		public void f() {System.out.println("f() in Bimple  of p185 with interface Aa.E.G");}
    		//Cannot reduce the visibility of the inherited method from Aa.E
    	}
    	protected class EHimple implements Aa.E.H{
    		public void f() {System.out.println("f() in Bimple  of p185 with interface Aa.E.H");}
    	}
    	public class Eimple2 implements Aa.E{
    		public void g() {System.out.println("g() in Eimple2 of p185 with interface Aa.E");}
    		public void g2() {System.out.println("g2() in Eimple2 of p185 with interface Aa.E");}
    		class EGimple2 implements Aa.E.G{
    			public void f() {System.out.println("f() in EGimple2 of interface Aa.E.G of Interface Aa.E in p185");}
    		}
    	}
    	
    	// main function
    	public static void main(String[] args) {
    		Aa obj=new Aa();
    		obj.receiveD(obj.getD());
    //		Aa.D  x2=obj.getD();//The type Aa.D is not visible
    //		Aa.Dimple x3=obj.getD();// cannot convert from Aa.D to Aa.Dimple
    //		obj.getD().f();//The type Aa.D is not visible
    //		Dimple d1=new Dimple();//Dimple cannot be resolved to a type
    //		Aa.Dimple2 d2=new Aa.Dimple2();
    		//No enclosing instance of type Aa is accessible.
    		//Must qualify the allocation with an enclosing instance of type Aa (e.g. x.new A() where x is an instance of Aa).
    	}
    }
    

     

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章