【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).
    	}
    }
    

     

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值