zoukankan      html  css  js  c++  java
  • 接口(二):

    接口的继承与类的继承区别:

    1、java类是单继承的,不可以同时继承,但是可以多重继承如果C想继承A和B,不可以写 C extends A,B,但是可以间接继承 如: A extends B 然后 C extends A 这样C 就相当于继承了A ,B

    2、接口可以多继承,一个接口可以同时继承多个直接父接口,如:C extends  A,B(A,B,C均为接口)

    3、与类继承相似,子接口继承某个父接口,可以获得父接口里定义的所有抽象方法、常量,如:

    interface InterfaceA{
    	int a=1;
    	void testA();
    }
    
    interface InterfaceB{
    	int b=2;
    	void testB();
    }
    
    interface InterfaceC extends InterfaceA,InterfaceB{
    	int c=3;
    	void testC();
    }
    
    public class InterfaceExtendsTest{
    	public static void main(String[] args){
    		System.out.println(InterfaceC.a);
    		System.out.println(InterfaceC.b);
    		System.out.println(InterfaceC.c);
    	}
    }
    

     运行结果:

    把上面代码稍微改动一下,体会其中本质的变化:

    interface InterfaceA{
    	int a=1;
    	void testA();
    }
    
    interface InterfaceB{
    	int b=2;
    	void testA();
    	void testB();
    }
    
    interface InterfaceC extends InterfaceA,InterfaceB{
    	int c=3;
    	void testC();
    }
    
    public class ClassExtendsInterface implements InterfaceC{
    	//-此处实现的是接口 InterfaceA 中的方法testA(),还是接口 InterfaceB 中的方法testA()?
    	public void testA(){
    			
    	}
    
    	public void testB(){
    			
    	}
    
    	public void testC(){
    			
    	}
    	
    	public static void main(String[] args){
    		
    		System.out.println(a);
    		System.out.println(b);
    		System.out.println(c);
    	}
    }
    

     运行结果:

    再把上面代码修改一下,仔细体会其中的变化:

    interface InterfaceA{
    	int a=1;
    	void testA();
    }
    
    interface InterfaceB{
    	int b=2;
    	void testA();
    	void testB();
    }
    
    interface InterfaceC extends InterfaceA,InterfaceB{
    	int c=3;
    	void testC();
    }
    
    public class ClassExtendsInterface implements InterfaceC{
    	//-此处把实现方法testA()屏蔽掉
    	//public void testA(){
    			
    	//}
    
    	public void testB(){
    			
    	}
    
    	public void testC(){
    			
    	}
    	
    	public static void main(String[] args){
    		
    		System.out.println(a);
    		System.out.println(b);
    		System.out.println(c);
    	}
    }
    

     编译结果:

    再把上面的代码修改一下:

    interface InterfaceA{
    	int a=1;
    	//-屏蔽掉接口InterfaceA中定义的方法testA()
    	//void testA();
    }
    
    interface InterfaceB{
    	int b=2;
    	void testA();
    	void testB();
    }
    
    interface InterfaceC extends InterfaceA,InterfaceB{
    	int c=3;
    	void testC();
    }
    
    public class ClassExtendsInterface implements InterfaceC{
    	//-此处把实现方法testA()屏蔽掉
    	//public void testA(){
    			
    	//}
    
    	public void testB(){
    			
    	}
    
    	public void testC(){
    			
    	}
    	
    	public static void main(String[] args){
    		
    		System.out.println(a);
    		System.out.println(b);
    		System.out.println(c);
    	}
    }
    

     编译结果:

    再把上面代码修改一下:

    interface InterfaceA{
    	int a=1;
    	void testA();
    }
    
    interface InterfaceB{
    	int b=2;
    	void testA();
    	void testB();
    }
    
    //-此处把继承接口InterfaceB,InterfaceA顺序修改一下
    interface InterfaceC extends InterfaceB,InterfaceA{
    	int c=3;
    	void testC();
    }
    
    public class ClassExtendsInterface implements InterfaceC{
    	//-此处把实现方法testA()屏蔽掉
    	//public void testA(){
    			
    	//}
    
    	public void testB(){
    			
    	}
    
    	public void testC(){
    			
    	}
    	
    	public static void main(String[] args){
    		
    		System.out.println(a);
    		System.out.println(b);
    		System.out.println(c);
    	}
    }
    

     编译结果:

    总结:

    对于继承多个直接父接口的接口来说,如果多个父接口中有相同的抽象方法,则子接口中只有一个该抽象方法,并且是按继承顺序的第一个父接口内的这个方法

  • 相关阅读:
    如何在百度文库里面免费下载东西
    CompareTo
    MySql常用日期函数(转载)
    Oracle之ORDER BY
    Spring之Ioc
    在使用与测绘有关软件中的困难
    HDOJ_1008_Elevator
    HDOJ_1005_Number Sequence
    HDOJ_1004_Let the Balloon Rise
    HDOJ_1003_MaxSum
  • 原文地址:https://www.cnblogs.com/baby-zhude/p/8157896.html
Copyright © 2011-2022 走看看