当前位置:网站首页>lambda expressions
lambda expressions
2022-04-23 06:01:00 【hanyc..】
In this case , From ordinary class -> Static inner class -> Local inner classes -> Anonymous inner class ->lambda expression , The overall complexity of the code is from complex to simple .
Functional interface : Any interface , If there is only one abstract method , So it's a functional interface .
For functional interfaces , We can go through lambda Expression to create the object of the interface
- lambda An expression can only be reduced to one line if it has only one line of code , If there are many lines , Then wrap it in code blocks
- The premise is that the interface is functional
- Multiple parameters can also be removed from the parameter type , If you want to get rid of everything , It has to be bracketed ( Parentheses can be omitted for a single parameter )
package creatthread;
public class TestLambda {
//2、 Static inner class
static class DrinkSpirit implements Drink {
@Override
public void haveADrink(String name, String drinkType) {
System.out.println(name + " Drinking " + drinkType);
}
}
public static void main(String[] args) {
//3、 Local inner classes
class DrinkFanta implements Drink {
@Override
public void haveADrink(String name, String drinkType) {
System.out.println(name + " Drinking " + drinkType);
}
}
System.out.println("============ Common classes implement interfaces ===============");
Drink drink1 = new DrinkCola();
drink1.haveADrink(" Zhang Yi ", " coke ");
System.out.println();
System.out.println("============ Static inner class ==============");
Drink drink2 = new DrinkSpirit();
drink2.haveADrink(" Li Er ", " Sprite ");
System.out.println();
System.out.println("============ Local inner classes ==============");
Drink drink3 = new DrinkFanta();
drink3.haveADrink(" The king of the three ", " Fanta ");
System.out.println();
System.out.println("============ Anonymous inner class ==============");
Drink drink4 = new Drink() {
//4、 Anonymous inner class
@Override
public void haveADrink(String name, String drinkType) {
System.out.println(name + " Drinking " + drinkType);
}
};
drink4.haveADrink(" Zhao si ", " coffee ");
System.out.println();
System.out.println("============lambda expression ==============");
//5、lambda expression
//lambda The expression returns an interface
Drink drink5 = (name, drinkType) -> System.out.println(name + " Drinking " + drinkType);
drink5.haveADrink(" Qian Wu ", " milk ");
}
}
interface Drink {
public void haveADrink(String name, String drinkType);
}
//1、 The interface is implemented through ordinary classes
class DrinkCola implements Drink {
@Override
public void haveADrink(String name, String drinkType) {
System.out.println(name + " Drinking " + drinkType);
}
}
result :
版权声明
本文为[hanyc..]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230541159237.html
边栏推荐
- 字符串(String)笔记
- Pytorch notes - observe dataloader & build lenet with torch to process cifar-10 complete code
- LDCT图像重建论文——Eformer: Edge Enhancement based Transformer for Medical Image Denoising
- 自定义异常类
- Paper on LDCT image reconstruction: edge enhancement based transformer for medical image denoising
- What is the difference between the basic feasible solution and the basic feasible solution in linear programming?
- Pytorch notes - complete code for linear regression & manual or automatic calculation of gradient code comparison
- 创建二叉树
- 去噪论文阅读——[RIDNet, ICCV19]Real Image Denoising with Feature Attention
- RedHat6之smb服务访问速度慢解决办法记录
猜你喜欢
Pytorch学习记录(七):处理数据和训练模型的技巧
CONDA virtual environment management (create, delete, clone, rename, export and import)
Configure domestic image accelerator for yarn
PyQy5学习(四):QAbstractButton+QRadioButton+QCheckBox
Pyqy5 learning (2): qmainwindow + QWidget + qlabel
Pytorch學習記錄(十三):循環神經網絡((Recurrent Neural Network)
JDBC连接数据库
Understand the current commonly used encryption technology system (symmetric, asymmetric, information abstract, digital signature, digital certificate, public key system)
PyQy5学习(二):QMainWindow+QWidget+QLabel
Reading of denoising papers - [cvpr2022] blind2blind: self supervised image denoising with visible blind spots
随机推荐
Linear algebra Chapter 1 - determinant
LDCT图像重建论文——Eformer: Edge Enhancement based Transformer for Medical Image Denoising
数字图像处理基础(冈萨雷斯)一
Pytorch学习记录(三):神经网络的结构+使用Sequential、Module定义模型
图解numpy数组矩阵
Development environment EAS login license modification
A general U-shaped transformer for image restoration
基于thymeleaf实现数据库图片展示到浏览器表格
Delete and truncate
rsync实现文件服务器备份
Manually delete registered services on Eureka
ValueError: loaded state dict contains a parameter group that doesn‘t match the size of optimizer‘s
PyQy5学习(四):QAbstractButton+QRadioButton+QCheckBox
深度学习基础——简单了解meta learning(来自李宏毅课程笔记)
常用编程记录——parser = argparse.ArgumentParser()
MySql基础狂神说
redhat实现目录下特定文本类型内关键字查找及vim模式下关键字查找
JVM series (3) -- memory allocation and recycling strategy
PyQy5学习(三):QLineEdit+QTextEdit
Pytorch学习记录(十二):学习率衰减+正则化