首页 > 编程语言 >Java8 新特性01-接口修饰-Lambda

Java8 新特性01-接口修饰-Lambda

时间:2022-10-28 13:01:27浏览次数:47  
标签:01 get void 接口 Java8 方法 public Lambda


Java8 新特性

文章目录

接口中默认的方法修饰为普通方法

在JDK8之前.​​interface​​之中可以定义变量和方法,变量必须是public,static, final的, 方法必须是public,abstract的, 由于这些修饰符都是默认的

  1. 接口定义方法: public抽象方法,需要子类实现
  2. 接口定义变量, public static final

在JDK1.8 开始, 支持使用static的default修饰 可以写方法体, 不需要子类重写

方法:

  1. 普通方法: 可以有方法体
  2. 抽象方法: 没有方法体需要子类实现,重写.
public interface JDKInterface {
/**
* 抽象方法 需要子类实现
* 默认类型:public、abstract
*/
void add();
void addOrder();

/**
* 默认方法 可以写方法体
*/
default void getDefaultOrder() {
System.out.println("我是默认的方法体");
}
}



public class JDKInterfaceImpl implements JDKInterface {
/**
* 默认的情况下必须重写接口中抽象方法 默认和静态方法是不需要必须重写
*/
@Override
public void add() {
}
@Override
public void addOrder() {
}

}

Lambda表达式

什么是lambda表达式

lambda好处: 简化我们匿名内部类的调用

lambda+方法引入 代码变得更加精简

为什么要使用Lambda表达式

可以非常简洁的形式调用我们的匿名函数接口。

public interface OrderService {
/**
* 如果需要使用OrderService接口
* 接口是无法new 可以通过匿名内部类new
* 定义子类
*/
void get();
}
public class Test02 {
public static void main(String[] args) {

// 1.使用new的实现类的形式调用接口
OrderService orderService = new OrderService() {
@Override
public void get() {
System.out.println("hhhhh");
}
};
orderService.get();

// 2.使用匿名内部接口调用
new OrderService() {
@Override
public void get() {
System.out.println("hhhhh");
}
}.get();

// 3.使用lambda调用接口
((OrderService) () -> System.out.println("hhhhh")).get();
}
}

Lambda表达式的规范

使用Lambda表达式 依赖于函数接口

  1. 在接口中只能允许使用一个抽象方法
  2. 在函数接口中定义object类中方法
  3. 使用默认或者静态方法
  4. ​@FunctionalInterface​​ 表示该接口为函数接口

Java中使用Lambda表达式的规范,必须是为函数接口

函数接口的定义:在该接口中只能存在一个抽象方法,该接口称作为函数接

函数接口定义

  1. 在接口中只能有一个抽象方法
  2. ​FunctionalInterface​​标记为接口为函数接口
  3. 可以通过default修饰普通方法
  4. 可以定义object类中的方法
@FunctionalInterface
public interface MyFunctionalInterface {
void add();

default void get() {

}

/**
* object父类中的方法可以在 函数接口中重写
*
* @return
*/
String toString();
}

Lambda基础语法

( ) — 参数列表

–> 分割

{} 方法体

(a,b)->{

}

  1. 无参方法调用
  2. 带参数方法调用
():函数方法参数列表
->分隔 {}方法体
(a,b)->{
Sout(a,b)
}

Lambda语法:
():参数列表
->分隔
{}:方法体
()->{}

无参方法调用

@FunctionalInterface
public interface AcanthopanaxInterface {
void get();
}
public class Test03 {
public static void main(String[] args) {
//1.使用匿名内部类调用
new AcanthopanaxInterface() {
@Override
public void get() {
System.out.println("get");
}
}.get();

//2.使用lamdba表达式调用方法
AcanthopanaxInterface acanthopanaxInterface = () -> {
System.out.println("使用lamdba表达式调用方法");
};
acanthopanaxInterface.get();
}
}

带参数方法调用

@FunctionalInterface
public interface YouShenInterface {
String get(int i, int j);
}
public class Test04 {
public static void main(String[] args) {
//1.使用匿名内部类调用
YouShenInterface youShenInterface = new YouShenInterface() {
@Override
public String get(int i, int j) {
return i + "-----" + j;
}
};
System.out.println(youShenInterface.get(2, 4));

//2.使用lamdba表达式调用方法
YouShenInterface you = (i, j) -> {
return i + "-----" + j;
};
System.out.println(you.get(2,6));

//3.使用lamdba表达式调用方法
((YouShenInterface)(i,j)->i+"---"+j).get(6,7);
}
}

精简语法

public class Test05 {
public static void main(String[] args) {
AcanthopanaxInterface acanthopanaxInterface = () -> System.out.println("我是方法");
acanthopanaxInterface.get();

//精简代码 // 使用lambda方法体中中只有一条语句的情况下
((AcanthopanaxInterface) () -> System.out.println("我是你别笔笔不")).get();
YouShenInterface youShenInterface = (i, j) -> {
return i + "++++++" + j;
};

// 使用Lambda 方法体中只有一条语句的情况下,在这时候我们不需要写{} 也可以不需要写return
String s = ((YouShenInterface) (i, j) -> i + "++++++" + j).get(4, 5);
System.out.println(s);
}
}

Lambda实战案例

案例一:
public class Test06 {
public static void main(String[] args) {
ArrayList<String> string = new ArrayList<>();
string.add("qqqq");
string.add("rrrrrr");
string.add("aaaaa");

//普通输出:
string.forEach(new Consumer<String>() {
@Override
public void accept(String s) {
System.out.println("0---"+s);
}
});

//Lambda:
string.forEach((o)->{
System.out.println(o);
});
}
}
案例二:
public class Test07 {
public static void main(String[] args) {
ArrayList<UserEntity> userLists = new ArrayList<>();
userLists.add(new UserEntity("cqqqq", 23));
userLists.add(new UserEntity("ooooo", 14));
userLists.add(new UserEntity("-----", 44));

//方法一: 排序 //普通内部类:
userLists.sort(new Comparator<UserEntity>() {
@Override
public int compare(UserEntity o1, UserEntity o2) {
return o1.getAge() - o2.getAge();
}
});

//方法一: 排序 //Lambda:
userLists.sort((o1, o2) -> o1.getAge() - o2.getAge());

userLists.forEach((t) -> {
System.out.println(t.toString());
});
}
}


标签:01,get,void,接口,Java8,方法,public,Lambda
From: https://blog.51cto.com/u_15850876/5804542

相关文章