关于函数式接口中的方法的类型的记录
函数式接口通过注解 @FunctionalInterface 来表示,
函数式注解只能有一个抽象方法,但是可以有多个默认方法,默认方法通过 default 标记
同时也可以有静态方法,具体测试代码如下
package com.atguigu.juc.day01; public class ThreadDemo { //线程创建的方式 //继承Thread //实现Runabble接口 public static void main(String[] args) { new myThread().start(); new Thread(new myRunable(),"A").start(); new Thread(new Runnable() { //匿名实现类实现 @Override public void run() { System.out.println(Thread.currentThread().getName()+"ookokok"); } },"B").start(); //lambda表达式:接口只能有一个抽象方法,和默认以及静态方法 new Thread(()->{ System.out.println(Thread.currentThread().getName()+"Okkk"); },"C..").start(); /* * 多线程模板 1.线程操作资源类 * * */ Foo f1 = (int a,int b)-> a+b; //实例化方法 lmada System.out.println("f1 = " + f1.add(1,2)); System.out.println("f2= = " + f1.sub(2,1)); System.out.println("f3= = " + f1.sum(2,1)); System.out.println("f3= = " + Foo.div(2,1)); //通过类名直接调用 } } @FunctionalInterface // 该注解表明当前接口为函数时接口,只能有一个抽象方法 interface Foo{ //接口中方法必须是抽象的或者是默认的以及静态的方法,默认的方法可以有多个, int add(int a,int b); //抽象方法 //int mul(int a,int b); default int sub(int a, int b){ //默认方法 return a-b; } default int sum(int a, int b){ //默认方法 return a+b; } static double div(int a,int b){ //静态接口 return a/b; } } class myRunable implements Runnable{ @Override public void run() { System.out.println(Thread.currentThread().getName()+"!!!"); } } class myThread extends Thread{ @Override public void run() { System.out.println(Thread.currentThread().getName()+"..."); } }