谈谈Java接口的演变

前言

演示代码来自:Private Methods in Java 9 Interfaces

JDK7及更低版本

接口可拥有:

  • 常量(默认带上 public static final 修饰符)
  • 抽象方法
// Java 7 program to illustrate 
// private methods in interfaces 
public interface TempI { 
    public abstract void method(int n); 
} 
  
class Temp implements TempI { 
    @Override
    public void method(int n) 
    { 
        if (n % 2 == 0) 
            System.out.println("geeksforgeeks"); 
        else
            System.out.println("GEEKSFORGEEKS"); 
    } 
  
    public static void main(String[] args) 
    { 
        TempI in1 = new Temp(); 
        TempI in2 = new Temp(); 
        in1.method(4); 
        in2.method(3); 
    } 
} 

输出结果:

geeksforgeeks
GEEKSFORGEEKS

JDK8

接口可拥有:

// Java 8 program to illustrate 
// static, default and abstract methods in interfaces 
public interface TempI { 
  
    public abstract void div(int a, int b); 
  
	public default void add(int a, int b) 
    { 
        System.out.print("Answer by Default method = "); 
        System.out.println(a + b); 
    } 
  
    public static void mul(int a, int b) 
    { 
        System.out.print("Answer by Static method = "); 
        System.out.println(a * b); 
    } 
} 
  
class Temp implements TempI { 
  
    @Override
    public void div(int a, int b) 
    { 
        System.out.print("Answer by Abstract method = "); 
        System.out.println(a / b); 
    } 
  
    public static void main(String[] args) 
    { 
        TempI in = new Temp(); 
        in.div(8, 2); 
        in.add(3, 1); 
        TempI.mul(4, 1); 
    } 
} 

输出结果:

Answer by Abstract method = 4
Answer by Default method = 4
Answer by Static method = 4

JDK9及更高版本

接口可拥有:

  • 常量
  • 抽象方法
  • 默认方法
  • 静态方法
  • 私有方法
  • 私有静态方法

注意:

  • 私有方法仅能在该接口内部调用,而不暴露给外界。
  • 不能在私有静态方法中使用私有非静态方法。
// Java 9 program to illustrate 
// private methods in interfaces 
public interface TempI { 
  
    public abstract void mul(int a, int b); 
  
	public default void add(int a, int b) 
    { 
		// private method inside default method 
        sub(a, b);  
  
		// static method inside other non-static method 
        div(a, b); 
        System.out.print("Answer by Default method = "); 
        System.out.println(a + b); 
    } 
  
    public static void mod(int a, int b) 
    { 
        div(a, b); // static method inside other static method 
        System.out.print("Answer by Static method = "); 
        System.out.println(a % b); 
    } 
  
    private void sub(int a, int b) 
    { 
        System.out.print("Answer by Private method = "); 
        System.out.println(a - b); 
    } 
  
    private static void div(int a, int b) 
    { 
        System.out.print("Answer by Private static method = "); 
        System.out.println(a / b); 
    } 
} 
  
class Temp implements TempI { 
  
    @Override
    public void mul(int a, int b) 
    { 
        System.out.print("Answer by Abstract method = "); 
        System.out.println(a * b); 
    } 
  
    public static void main(String[] args) 
    { 
        TempI in = new Temp(); 
        in.mul(2, 3); 
        in.add(6, 2); 
        TempI.mod(5, 3); 
    } 
} 

输出结果:

Answer by Abstract method = 6              // mul(2, 3) = 2*3 = 6
Answer by Private method = 4               // sub(6, 2) = 6-2 = 4 
Answer by Private static method = 3        // div(6, 2) = 6/2 = 3
Answer by Default method = 8               // add(6, 2) = 6+2 = 8
Answer by Private static method = 1        // div(5, 3) = 5/3 = 1 
Answer by Static method = 2                // mod(5, 3) = 5%3 = 2
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值