Java中的Interface

Interface(接口)是什么?
Interface与Class 属于同一个层次,其实尽可以把接口看作是特殊的抽象类。
创建接口的方法与创建类的方法相同,只是把关键字从Class变成了Interface。

初识interface

import static net.mindview.util.Print.*;

interface FirstInterface {

    int ID = 100;
    // 接口声明两个方法 但不能给出实现
    public void sayHi();

    public void sayBye();
}

/**
 * @author xus
 * @date 2015-10-26 TODO implements关键字使用 interface 实现类被要求必须实现interface中的方法
 **/
public class TestInterface implements FirstInterface {

    @Override
    public void sayHi() {

        println("I'm interface, sup?");
    }

    @Override
    public void sayBye() {
        println("You are so boring, see ya");
    }

    public static void main(String[] args){
        TestInterface testInterface = new TestInterface();
        println("ID = " + testInterface.ID);
        testInterface.sayHi();
        testInterface.sayBye();
    }

}

/**
    输出

     ID = 100
     I'm interface, sup?
     You are so boring, see ya

 */

从这个例子可以看出interface使用起来是非常简单的,但是我为什么要大费周折的去使用interface呢?直接在实现类里面包含这两个方法不就行了嘛?如果你有这个疑问的话,说明你还没开始体会到interface的强大之处。
现在假设这么一种情况,你有1000个实现类 ,每个类都需要sayHi(),sayBye()方法 , 那么你是不是打算编程的时候考虑每个类的具体实现是什么样?
interface就是帮助你把这些抽象出来,减少复杂度。

interface 细节

    @Override
    public void sayHi() {

        println("I'm interface, sup?");
    }

    @Override
    public void sayBye() {
        println("You are so boring, see ya");
    }

如果你仔细看了上面的代码,你也许会注意到重写的 interface 方法 全部都是public类型,interface 定义的时候,要求默认方法就是public类型。
interface中,我们定义的int ID; 默认为static & final类型。

interface的多继承

Java中的类虽然 不支持多继承,但是interface确实支持多继承。 通过interface的多继承,我们能很方便的扩充interface的功能。

interface InterfaceOne{
    void one();
}
interface InterfaceTwo{
    void two();
}
interface InterfaceThree{
    void three();
}
interface InterfaceFour extends InterfaceOne,InterfaceTwo,InterfaceThree{
    void four();

}

public class MultipleInheritanceInterface implements InterfaceFour{

    @Override
    public void one() {
        // TODO Auto-generated method stub

    }

    @Override
    public void two() {
        // TODO Auto-generated method stub

    }

    @Override
    public void three() {
        // TODO Auto-generated method stub

    }

    @Override
    public void four() {
        // TODO Auto-generated method stub

    }

}

interface的名称冲突

interface InterfaceNameOne {
    void uniqueName();
}

interface InterfaceNameTwo {
    int uniqueName(int i);
}

interface InterfaceNameThree {
    int uniqueName();
}

public class InterfaceCollision implements InterfaceNameOne, InterfaceNameTwo,InterfaceNameThree {

    //报错
    @Override
    public void uniqueName() {
        println("This is a real unique name");
    }

    //报错
    @Override
    public void uniqueName(int i) {
        // TODO Auto-generated method stub

    }

    public static void main(String[] args) {

        InterfaceCollision interfaceCollision = new InterfaceCollision();
        interfaceCollision.uniqueName();
    }

}

在编写interface的时候,很可能遇到这方面的陷阱,如果implements 多个interface且这些interface存在多个方法,就会导致代码冲突 ,从而引起混乱,所以尽量避免这种情况的出现。

interface default方法,可实现的方法

interface TestInterface {
    default void printLove() {
        print("这是一个已经实现的方法");
    }
}

public class DefaultInterfaceFounction implements TestInterface {

    public static void main(String[] args) {
        DefaultInterfaceFounction test = new DefaultInterfaceFounction();
        test.printLove();
    }
}

上面代码示例里面的 printLove()方法在interface中就 已经被实现。使用default方法有什么好处呢?(请一定要多思考)
假设我们现在还是有1000个实现类 , 每个类都implements了 MyInterface 这个接口, 那么我现在想在MyInterface中添加一个printLove()方法给其中的100个实现类使用,剩下的900个实现类不需要printLove()方法。这个时候就是default方法展现本领的时候了,interface中的default方法 ,实现类无需被强制实现。

interface的实例化

下面这段代码来自Think in java.
interface虽然不能被实例化,但是interface可以在其实现类中进行实例化。

interface Service {
    void method1();
    void method2();
}

interface ServiceFactory {
    Service getService();
}

class Implementation1 implements Service {
    Implementation1() {
    } // Package access

    public void method1() {
        print("Implementation1 method1");
    }

    public void method2() {
        print("Implementation1 method2");
    }
}

class Implementation1Factory implements ServiceFactory {
    public Service getService() {
        return new Implementation1();
    }
}

class Implementation2 implements Service {
    Implementation2() {
    } // Package access

    public void method1() {
        print("Implementation2 method1");
    }

    public void method2() {
        print("Implementation2 method2");
    }
}

class Implementation2Factory implements ServiceFactory {
    public Service getService() {
        return new Implementation2();
    }
}

public class Factories {
    public static void serviceConsumer(ServiceFactory fact) {
        Service s = fact.getService();
        s.method1();
        s.method2();
    }

    public static void main(String[] args) {
        serviceConsumer(new Implementation1Factory());
        serviceConsumer(new Implementation2Factory());
        print(new Implementation1Factory() instanceof ServiceFactory);
    }
}

/**
    输出
    Implementation1 method1
    Implementation1 method2
    Implementation2 method1
    Implementation2 method2
    true

 */

Java日记
2015年10月26日 星期一 遇见你真好。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值