zoukankan      html  css  js  c++  java
  • [译]Java例解Interface

    受不了xxxx恶心人的行为,遂搬迁至博客园。
    始发:2016-01-10 17:58:52
    

    因为经常需要接触Java代码,加上该篇文章对Interface讲述思路清晰、例子简洁(简洁是智慧的灵魂——by莎翁)、内容全面。原文等可以FQ了在贴。

    之前我们叙说过抽象类(abstract 类),用来实现软件中的抽象机制(即,隐藏那些对用户不相干的细节的一种机制)。但是抽象类实现的是不完全的(partial abstraction)抽象机制,接下来要讨论的 interface 则实现了完整的抽象机制。涉及:

    1、interface 是什么

    2、interface 的重要性

    3、怎么用 interface?使用中的细节?

    一、interface 是什么

    interface 代码段长得像类,但本质上它并不是。interface 和类一样拥有自己的方法(methods)和变量(variables),但声明在 interface 中方法默认是抽象的,只有方法签名,没有实现该方法。至于声明的变量,默认属性是 publicstaticfinal,接下来的部分会详细讨论这些方面。



    二、interface 用来做什么

    上面文章曾总结性的说过:interface 用来实现抽象机制。鉴于 interface 中方法都没有实现其具有的功能,所以使用前必须先实现这些个方法,而且 implement 这个 interface 的类必须实现这个 interface 中的所有方法。我们知道,Java 不支持多重继承(multiple inheritance),使用 interface 则可以实现多重继承——一个类可以 implement 多个 interface

    三、怎么用 interface

    1、interface 的声明

    Java 使用“interface”关键字声明一个 interface,例如:

    interface MyInterface
    {
       /* All the methods are public abstract by default
        * Note down that these methods are not having body
        */
       public void method1();
       public void method2();
    }

    2、interface 的实现

    以下例子展示了一个类如何实现一个 interface,该类必须提供 interface 中声明的所有方法的具体实现:

    interface MyInterface
    {
       public void method1();
       public void method2();
    }
     
    class XYZ implements MyInterface
    {
      public void method1()
      {
          System.out.println("implementation of method1");
      }
      public void method2()
      {
          System.out.println("implementation of method2");
      }
      public static void main(String arg[])
      {
          MyInterface obj = new XYZ();
          obj. method1();
      }
    }

    运行输出如下:

    implementation of method1
    

    3、interface 和继承的关系

    interface 可以继承(extends)另一个 interface,但不能实现(implement)另一个 interface。需要的话,只能 extends 其他 interface。下面的例子有两个interface,Inf1、Inf2,Inf2 通过继承的方式 extends Inf1,如果有类 implement Inf2,它必须实现 Inf1Inf2 所有方法。

    public interface Inf1{
       public void method1();
    }
     
    public interface Inf2 extends Inf1 {
       public void method2();
    }
     
    public class Demo implements Inf2 {
      public void method1(){
        //Implementation of method1
      }
      public void method2(){
        //Implementation of method2
      }
    }

    以上代码,因为“Demo”类 implements Inf2,所以它也必须实现 Inf1 中的方法,因为 Inf2 extends Inf1

    四、interface 使用中的关键点

    (1)在 Java 中,不能实例化(instantiate)一个 interface

    (2)interface 提供了真正意义上的完全的抽象机制实现途径,因为它的所有方法都没有提供具体实现。另外,我们说抽象类提供了抽象机制的不完全实现是因为在类中,抽象方法和具体方法(concrete methods,methods with body)可以共存。

    (3)implements 关键字用来让类实现某个 interface

    (4)类提供了 interface 中方法的具体实现,这些方法需要用 public 修饰。

    (5)类必须实现 interface 中的所有方法,否则该类应该声明为抽象类“abstract”。

    (6)interface 不能被如下关键字修饰:privateprotectedtransient

    (7)interface 中所有方法默认属性:abstractpublic

    (8)interface 中所有变量默认属性:publicstatic and final

    interface Try
    {
       int a=10;
       public int a=10;
       public static final int a=10;
       final int a=10;
       static int a=0;
    }

    以上代码中所有变量都是同样性质。

    (9)interface 中的所有变量都必须在声明时初始化(initialized),否则编译器会抛出错误。

    interface Try
    {
          int x;//Compile-time error
    }

    (10)在实现 interface 的类中,不能更改 interface 中声明的变量的值,原因见(8)。一旦试着更改之,编译器抛出错误。

    Class Sample implements Try
    {
      public static void main(String arg[])
      {
         x=20; //compile time error
      }
    }

    (11)任何 interface 都可以 extends 其他任何 interface,但不能 implements

    (12)一个类可以 implements 无数个 interface

    (13)如果几个 interface 中包含完全相同的方法,在类中只需要实现一次该方法。

    interface A
    {
       public void aaa();
    }
     
    interface B
    {
       public void aaa();
    }
     
    class Central implements A,B
    {
       public void aaa()
       {
            //Any Code here
       }
       public static void main(String arg[])
       {
            //Statements
        }
    }

    (14)如果几个 interface 中包含同名方法,但是返回值不一样,类对此无能为力。

    interface A
    {
       public void aaa();
    }
     
    interface B
    {
       public int aaa();
    }
     
    class Central implements A,B
    {
     
       public void aaa() // error
       {
       }
       public int aaa() // error
       {
       }
       public static void main(String arg[])
       {
     
       }
    }

    (15)如果几个 interface 中包含重名的变量,可以通过前置 interface 名称解决这个冲突。

    interface A
    {
        int x=10;
    }
     
    interface B
    {
        int x=100;
    }
     
    class Hello implement A,B
    {
        public static void Main(String arg[])
        {
     
           System.out.println(x); // reference to x is ambiguous both variables are x
           System.out.println(A.x);
           System.out.println(B.x);
        }
    }

     

    五、interface 的好处

    以下就是 interface 的优势:

    无需为 interface 中方法的具体实现操心;

    Java 中不允许多重继承,通过 interface 就可以实现同样的目的;

    一个类只能继承一个类但可以 implements 无数个 interface

    最后:

    It saves you from Deadly Diamond of Death(DDD) problem.

    何为 DDD 参见: In C++, what’s the diamond problem, and how can it be avoided?

  • 相关阅读:
    List装form
    《Java设计模式》之调停者模式(Mediator)
    android 4.0 禁用系统home键
    最大权二分匹配
    hdu 3667 /2010哈尔滨赛区H题 费用与流量为非线性关系/费用流
    【python】filter()
    【python】linux将python改为默认3.4版本
    【linux】VMware12.0安装
    【python】lxml-The E-factory
    【xml】python的lxml库使用
  • 原文地址:https://www.cnblogs.com/rockyching2009/p/13129235.html
Copyright © 2011-2022 走看看