The Default Methods And Static Methods In Java Interface

Default Methods
Default methods enable you to add new functionality to the interfaces of your libraries and ensure binary compatibility with code written for older versions of those interfaces

默认方法主要是在确保不破坏原有程序的基础上对接口进行扩展 :

public interface TInterface {
    //原有一个sayHello()方法。
    void sayHello();
}
/**
 * TImplement 实现了 TInterface接口,并重写了sayHello()方法
 * @author Administrator
 *
 */
public class TImplement implements TInterface {

    @Override
    public void sayHello() {
        // TODO Auto-generated method stub
        System.out.println("Hello");

    }

}
/**
 * AImplement 也实现了 TInterface接口,但是现在需要在AImplement中新增greeting()方法,并希望在接口中实现
 * 而且需要兼容以前的程序,要实现这个功能,解决方案不是单一的,例如可以另写一个接口继承TInterface接口,
 * 然后AImplement继承新的接口,但Java8中对Interface新增了Default方法,我们可以在不另加其他接口的情
 * 况下对原有接口进行扩展
 * 
 * @author Administrator
 *
 */
public class AImplement implements TInterface {

    @Override
    public void sayHello() {
        // TODO Auto-generated method stub
        System.out.println("Say Hello!");

    }

}

修改原来的TInterface接口 ,新增greeting()方法。

public interface TInterface {
    //原有一个sayHello()方法。
    void sayHello();
    //新增greeting()方法,并编写默认实现代码
    default public void greeting(){
        System.out.println("Say Greeting");
    }
}

在AImplement中重写greeting()方法,覆盖默认方法。

/**
 * AImplement 也实现了 TInterface接口,但是现在需要在AImplement中新增greeting()方法,并希望在接口中实现
 * 而且需要兼容以前的程序,要实现这个功能,解决方案不是单一的,例如可以另写一个接口继承TInterface接口,然后AImplement
 * 继承新的接口,但Java8中对Interface新增了Default方法,我们可以在不另加其他接口的情况下对原有接口进行扩展
 * 
 * @author Administrator
 *
 */
public class AImplement implements TInterface {

    @Override
    public void sayHello() {
        // TODO Auto-generated method stub
        System.out.println("Say Hello!");

    }

    /* 
     * 重写greeting()方法,以适应不同的需求
     */
    @Override
    public void greeting() {
        // TODO Auto-generated method stub
        System.out.println("Override!");
    }


}

此时我们已经完成在不破坏原有程序的基础之上对接口进行了扩展。

public static void main(String[] args) throws InstantiationException, IllegalAccessException {
        AImplement.class.newInstance().greeting();
    }

测试输出:Override!


Static Methods
A static method is a method that is associated with the class in which it is defined rather than with any object. Every instance of the class shares its static methods.

Static Methods可以在接口中定义Static方法,并且每一个实例(该接口)都共享这些方法。

/**
 * static 方法无法override,该接口的每个实例都将共享这个say()方法。
 * 
 * @author Administrator
 *
 */
public interface TInterface {
    // Static Method 
    static public String say(){
        return "123456";
    }
}

直接使用接口调用该方法

    public static void main(String[] args) {
        //使用接口直接调用该方法
        System.out.println(TInterface.say());

    }

输出结果:123456

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值