java请求接口示例_Java 8:功能接口示例

java请求接口示例

为了支持Java 8中的lambda表达式,他们引入了Functional Interfaces。

具有单一抽象方法的接口可以称为功能接口。

Runnable,Comparator,Cloneable是功能接口的一些示例。 我们可以使用Lambda表达式实现这些功能接口。

例如:

Thread t =new Thread(new Runnable(){
   public void run(){
     System.out.println("Runnable implemented by using Lambda Expression");
   }
});

这是创建线程的旧方法。

由于Runnable具有单一抽象方法,我们可以将其视为功能接口,并且可以使用如下所示的Lambda表达式。

Thread t = new Thread(()->{
   System.out.println("Runnable implemented by using Lambda Expression");
});

在这里,我们没有传递Runnable对象,而是传递了lambda表达式。

声明我们自己的功能接口:

我们可以通过限定了S个英格尔一个 bstract M在接口ethod宣布我们自己的功能界面。

public interface FunctionalInterfaceTest{
void display();
}
//Test class to implement above interface
public class FunctionInterfaceTestImpl {
      public static void main(String[] args){
     //Old way using anonymous inner class
     FunctionalInterfaceTest fit = new FunctionalInterfaceTest(){
        public void display(){
           System.out.println("Display from old way");
        }};
     fit.display();//outputs: Display from old way
     //Using lambda expression
     FunctionalInterfaceTest newWay = () -> {System.out.println("Display from new Lambda Expression");}
        newWay.display();//outputs : Display from new Lambda Expression
     }
}

我们可以使用@FunctionalInterface批注进行批注,以告知编译时错误。 它是可选的

例如:

@FunctionalInterface
public interface FunctionalInterfaceTest{
   void display();
   void anotherDisplay();//shows an error, FunctionalInterface should have only one abstarct method.
}

默认方法:

功能接口不能具有多个抽象方法,但是可以具有多个默认方法。

Java 8中引入了默认方法,以添加新方法进行接口,而不会干扰已实现的类。

interface DefaultInterfaceTest{
  void show();
  default void display(){
     System.out.println("Default method from interface can have body..!");
  }
}
public class DefaultInterfaceTestImpl implements DefaultInterfaceTest{
   public void show(){
         System.out.println("show method");
   }
   //we dont need to provide any implementation to default method.
   public static void main(String[] args){
          DefaultInterfaceTest obj = new DefaultInterfaceTestImpl();
          obj.show();//out puts: show method
          obj.display();//outputs : Default method from interface can have body..!
        }
}

默认方法的主要用途是不强制实现类,我们可以向接口添加方法。

多重继承:

如果两个接口中存在相同的默认方法,并且一个类正在实现该接口,则它将引发错误。

//Normal interface with show method

interface Test{

  default void show(){
     System.out.println("show from Test");
  }

}

//Another interface with same show method

interface AnotherTest{

   default void show(){
      System.out.println("show from Test");
   }

}

//Main class to implement above two interfaces

class Main implements Test, AnotherTest{
//here is an ambiguity which show method has to inherit here
}

此类不会编译,因为Test和AnotherTest接口show()方法之间存在歧义,要解决此问题,我们需要将Show()方法覆盖到Main Class。

class Main implements Test, AnotherTest{

   void show(){
      System.out.println("Main show method");
   }

}

翻译自: https://www.javacodegeeks.com/2014/05/java-8-functional-interface-example.html

java请求接口示例

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值