当前位置: 首页 > news >正文

赣icp上饶网站建设河南旅游网页设计

赣icp上饶网站建设,河南旅游网页设计,房子装修改造,wordpress不兼容ie8Lambda表达式用法汇总 java8 中引入的 Lambda 表达式真的是个好东西#xff0c;掌握之后#xff0c;写代码更简洁了#xff0c;码字效率也提升了不少#xff0c;这里咱 们一起来看看 Lambada 表达式常见的写法#xff0c;加深理解。 1、有参无返回值函数式接口 8 种写法…Lambda表达式用法汇总 java8 中引入的 Lambda 表达式真的是个好东西掌握之后写代码更简洁了码字效率也提升了不少这里咱 们一起来看看 Lambada 表达式常见的写法加深理解。 1、有参无返回值函数式接口 8 种写法 1.1、先来定义一个函数式接口 有 2 个参数无返回值 FunctionalInterface public interface Fun {void print(String s1, String s2); }创建 Fun 有 8 种写法。 1.2、第 1 种原始的写法如下 Fun fun1 new Fun() {Overridepublic void print(String s1, String s2) {String msg s1 , s2;System.out.println(msg);} };1.3、第 2 种Lambda 方式完整写法 Fun fun2 (String s1,String s2) - {String msg s1 , s2;System.out.println(msg); };1.4、第 3 种Lambda 方式隐藏参数类型 Fun fun3 (s1, s2) - {String msg s1 , s2;System.out.println(msg); };1.5、第 4 种Lambda 方式单行可去掉{} Fun fun4 (s1, s2) - System.out.println(s1 , s2);1.6、第 5 种Lambda 方式引用静态方法 添加一个静态方法 public static class StaticMethod {public static void print(String s1, String s2) {String msg s1 , s2;System.out.println(msg);} }继续简写 Fun如下 Fun fun5 (s1, s2) - StaticMethod.print(s1,s2);1.7、第 6 种Lambda 方式使用::引用静态方法隐藏参数 若静态方法的参数和函数式接口的方法参数一致可以对下面代码继续优化 Fun fun5 (s1, s2) - StaticMethod.print(s1,s2);可以优化为下面这样 Fun fun6 StaticMethod::print;1.8、第 7 种Lambda 方式引用实例方法 添加一个实例对象内部添加一个实例方法 public static class InstanceMethod {public void print(String s1, String s2) {String msg s1 , s2;System.out.println(msg);} }继续简写 Fun如下 InstanceMethod instanceMethod new InstanceMethod(); Fun fun7 (s1, s2) - instanceMethod.print(s1,s2);1.9、第 8 种Lambda 方式使用::实例态方法隐藏参数 若实例方法的参数和函数式接口的方法参数一致可以对下面代码继续优化 Fun fun7 (s1, s2) - instanceMethod.print(s1,s2);优化为下面这样 Fun fun8 instanceMethod::print;2、无参有返回值函数式接口 8 种写法 2.1、来一个有返回值的函数式接口 函数式接口 UserFun 中有个 createUser 方法用来创建 User 对象User 类有 2 个属性定义了 2 个构造方法StaticMethod 提供了一个静态方法 user()用来创建 User 对象InstanceMethod 提供了一个实例方法 user()用来创建 User 对象 FunctionalInterface public interface UserFun {User createUser(); }public static class User {private String name;private Integer age;public User() {}public User(String name, Integer age) {this.name name;this.age age;} } public static class StaticMethod {public static User user() {return new User(张三, 30);} }public static class InstanceMethod {public User user() {return new User(张三, 30);} }创建 UserFun 有 8 种方式下面来看各种方式演化的过程。 2.2、创建 UserFun 的 8 种写法 Test public void test1() {//方式1原始方式UserFun fun1 new UserFun() {Overridepublic User createUser() {return new User(张三, 30);}};//方式2Lambda玩转方式()-{方法体;}UserFun fu2 () - {return new User(张三, 30);};//方式3方法体只有一行可去掉{}UserFun fun3 () - new User(张三, 30);//方式4调用静态方法UserFun fun4 () - {return StaticMethod.user();};//方式5去掉{}UserFun fun5 () - StaticMethod.user();//方式6使用::调用静态方法UserFun fun6 StaticMethod::user;InstanceMethod instanceMethod new InstanceMethod();//方式7实例方法UserFun fun7 () - instanceMethod.user();//方式8实例对象::实例方法UserFun fun8 instanceMethod::user; }2.3、第 9 种方式类::new 的 代码如下下面 2 种创建方式类似第一种是第二种的简写。 //方式9类::new的方式 UserFun fun9 User::new; //这个相当于 UserFun fun10 new UserFun(){Overridepublic User createUser() {return new User();} };3、有参有返回值的函数式接口 3.1、来一个有参、有返回值的函数式接口 函数式接口 UserFun 中有个有参的 createUser(String name,Integer age)方法用来创建 User 对象User 类有 2 个属性定义了 1 个有参构造方法StaticMethod 提供了一个静态方法 user(String name,Integer age)用来创建 User 对象InstanceMethod 提供了一个实例方法 user(String name,Integer age)用来创建 User 对象 FunctionalInterface public interface UserFun {User createUser(String name, Integer age); }public static class User {private String name;private Integer age;public User(String name, Integer age) {this.name name;this.age age;} }public static class StaticMethod {public static User user(String name, Integer age) {return new User(name, age);} }public static class InstanceMethod {public User user(String name, Integer age) {return new User(name, age);} }3.2、创建 UserFun 的 11 种写法 //方式1原始方式 UserFun fun1 new UserFun() {Overridepublic User createUser(String name, Integer age) {return new User(name, age);} };//方式2Lambda玩转方式()-{方法体;} UserFun fun2 (String name, Integer age) - {return new User(name, age); };//方式3方法体只有一行可去掉{} UserFun fun3 (String name, Integer age) - new User(张三, 30);//方式4隐藏参数类型 UserFun fun4 (name, age) - new User(张三, 30);//方式5调用静态方法 UserFun fun5 (String name, Integer age) - {return StaticMethod.user(name, age); };//方式6隐藏参数类型单行可以去掉{}和return UserFun fun6 (name, age) - StaticMethod.user(name, age);//方式7使用::调用静态方法参数类型数量需要和静态方法一致 UserFun fun7 StaticMethod::user;InstanceMethod instanceMethod new InstanceMethod(); //方式8调用实例方法 UserFun fun8 (String name, Integer age) - {return instanceMethod.user(name, age); };//方式9隐藏参数类型单行可以去掉{}和return UserFun fun9 (name, age) - instanceMethod.user(name, age);//方式10使用::调用静态方法参数类型数量需要和实例方法一致 UserFun fun10 instanceMethod::user;//方式11类::new的方式函数接口中的方法参数需要和User类中构造方法的参数类型数量一致 UserFun fun11 User::new; //这个相当于 UserFun fun12 new UserFun(){Overridepublic User createUser(String name,Integer age) {return new User(name,age);} };4、无参无返回值 Runnable a1 () - System.out.println(Hello); new Thread(a1).start(); a1.run();5、特殊案例 下面这个案例比较特殊下面列出了 5 种写法这几种写法效果是一样的。 方式 5 比较特殊注意看注释大家领会领会。 public class LambdaTest4 {FunctionalInterfacepublic interface Fun {String dispose(String s1, String s2);}Testpublic void test1() {//方式1原始写法Fun fun1 new Fun() {Overridepublic String dispose(String s1, String s2) {return s1.concat(s2);}};//方式2lambda玩转写法Fun fun2 (String s1, String s2) - {return s1.concat(s2);};//方式3单行可以隐藏{}和returnFun fun3 (String s1, String s2) - s1.concat(s2);//方式4隐藏参数Fun fun4 (s1, s2) - s1.concat(s2);/*** 方式5类::方法这种写法需要满足* 1、函数接口方法的第1个参数类型需要和{类名::方法}中的类名是同种类型* 2、第1个参数后面的参数将作为{类名::方法}中方法的参数注意下面的concat方法只有1个参数*/Fun fun5 String::concat;} }6、使用 6.1、有参无返回值函数式接口 package com.example.lambdatest;/*** author zhangshixing* 有参无返回值函数式接口*/ public class Test1 {public static void main(String[] args) {Fun fun (String s1, String s2) - {String msg s1 , s2;System.out.println(msg);};// hello,worldfun.print(hello, world);}FunctionalInterfacepublic interface Fun {void print(String s1, String s2);} }6.2、无参有返回值函数式接口 package com.example.lambdatest;/*** author zhangshixing* 无参有返回值函数式接口*/ public class Test2 {public static void main(String[] args) {Fun fun () - Hello,World;String returnStr fun.getNumber();System.out.println(returnStr);}FunctionalInterfacepublic interface Fun {String getNumber();} }6.3、有参有返回值的函数式接口 package com.example.lambdatest;/*** author zhangshixing* 有参有返回值的函数式接口*/ public class Test3 {public static void main(String[] args) {Fun fun (a, b) - a b;int result fun.add(1,2);System.out.println(result);}FunctionalInterfacepublic interface Fun {int add(int a, int b);} }6.4 无参无返回值 Runnable a1 () - System.out.println(Hello); new Thread(a1).start(); a1.run();
http://www.zqtcl.cn/news/839926/

相关文章:

  • 龙文网站建设有域名可以自己做网站吗
  • 东莞优化网站建设肥猫网站建设
  • 东莞住房和建设局网站dedecms如何做网站
  • 广州商城网站建设地址义马网站开发
  • 全球购物网站排名高端网站定制开发设计制作
  • 软件开发专业课程有哪些seo比较好的优化
  • 重庆网站建设坤思特seo关键词报价查询
  • 重庆装修公司排名前十口碑推荐南京做网站seo
  • 佛山网站优化美姿姿seo网站策划方案 优帮云
  • 阿里巴巴国际站网站做销售方案东莞营销推广
  • 电子商城网站开发流程wordpress 文章发布时间
  • 莆田建网站公司盱眙县住房和城乡建设局网站
  • 2018年的网站制作室内设计网站哪些号
  • 做网站有包括哪些东西抖音seo关键词优化排名
  • 网站建设费无形资产做招聘网站需要什么
  • 长沙企业做网站网页制作教程免费下载
  • 重庆北碚网站建设空包网站分站怎么做
  • 北京神州网站建设湖北响应式网站建设费用
  • 环保网站设计价格建设网站对公司起什么作用
  • 做乒乓球网站的图片大全学网页设计哪个培训学校好
  • 婚礼做的好的婚庆公司网站用手机能创建网站吗
  • 广州网站开发平台.net做的网站代码
  • 地图网站设计建立公司网站视频
  • 哪个网站可以做销售记录仪中国电子商务中心官网
  • 学校网站建设厂家云上铺会员管理系统
  • 手机网站源码大全空间设计公司
  • 公司做哪个网站比较好招聘网站企业招聘怎么做
  • 北仑网站推广用c 做网站
  • 做网站怎么赚钱 注册网站环境配置
  • 阿里企业网站建设重庆移动网站制作