• 接口(三)——JAVA的多重继承


    一、接口的作用

    ①、为了能够向上转型为多个基类型

    ②、防止客户端程序员创建该类的对象——同抽象类

    二、通过继承扩展接口

    interface Monster{
       void menace();  
    }
    
    interface Lethal{
      void kill();
    }
    
    //继承接口 但用的是extends
    interface DangerousMonster extends Monster{
       void destory();  
    }
    
    //支持多继承
    interface Vampire extends Lethal,Monster{
      void drinkBlood();
    }

    三、组合接口的名字冲突

    //接口和类具有相同的方法
    interface CanFight{
      void fight();
    }
    
    class ActionCharacter{
      public void fight(){}
    }
    
    //Hero可以不许要重写void fight()也可以创建
    class Hero extends ActionCharacter implements CanFight{    
    }

    当如果方法名一样,签名和返回值都不一样:

    //接口和类具有相同的方法,不同的返回值
    interface CanFight{
      int fight();
    }
    
    class ActionCharacter{
      public void fight(){}
    }
    
    class Hero extends ActionCharacter implements CanFight{    
    }
    //直接编译报错:说明重载方法通过返回值是区分不开的

    四:接口的适配模式

    对象①获取已继承接口③的类②的对象,通过类②向上转型并调用类②的方法。

    五:接口中的域

    ①、放入接口中的任何成员变量域都自动是static 和 final

    ②、接口中的方法都自动是Public

    ③、接口中的成员变量不能够为空,一定要被初始化(可以是非常量表达式)

    public interface RandVlas{
       int RANDOM_INT = new Random().nextInt(10);
    }

    ④、因为是static的所以会在任何域首次被访问的时候初始化。

    六、嵌套接口

     (一)、接口可以嵌套在类或者其他接口中

    //接口嵌套在类中
    class A{
      interface B{
        void f();
      }  
    }
    
    //接口嵌套在其他接口中
    interface E{
      interface G{
        void f();
      }
      //接口自带的方法
      void g();
    }
    嵌套举例

    如何调用其中的接口:

    //调用声明在类中的接口
    public class BImp implements A.B{
      public void f(){}
    }
    
    
    //调用声明在接口中的接口
    public class EGImp implements E.G{
      public void f(){}
    } 
    //E接口直接声明的方法
    public class EImp implements E{
      public void g(){}
    }
    调用接口

    注意事项:

    1.嵌套在另一个接口的接口自动为Public

    2.实现某个接口的时候不需要嵌套实现其内部的接口

    (二)、接口被实现为private,其优点

    举例:前期准备

    public class Test {
        private A mA;
        
        //声明A接口
        private interface A{
            public void f();
        }
        //继承接口的实现类
        public class AImp implements A{
            @Override
            public void f() {
                // TODO Auto-generated method stub
                
            }
        }
        //外界获取A
        public A getA(){
            return new AImp();
        }
        //接收A,并调用其方法
        public void receiveA(A a){
            mA = a;
            mA.f();
        }
    }
    Test.java

    调用

    public class NestingInterfaces {
    
        public static void main (String args[]){
            Test t1 = new Test();
            Test t2 = new Test();
            t2.receiveA(t1.getA());
            /*错误:A a = t1.getA();
             *    t1.getA().f();
             * */
        }
    
    }
    NestingInterfaces

    作用:

    Test.AImp只能被其自身使用,无法说其实现了private 接口A。强制类不允许向上转型。
    getA()可以将返回值交给有权利使用它的对象receiveA()

    七、接口与工厂方法(详见HeadFirst设计模式)

  • 相关阅读:
    BZOJ2457 双端队列 题解
    POJ1723,1050,HDU4864题解(贪心)
    Splay与FHQ-Treap
    POJ3179 Corral the Cows题解
    使用easypoi根据表头信息动态导出excel
    Spring @Configuration注解
    vue脚手架vue-cli的搭建
    使用poi导出excel
    mybatis中的一对多和多对一
    angularjs模态框的使用
  • 原文地址:https://www.cnblogs.com/rookiechen/p/5413657.html
Copyright © 2020-2023  润新知