0
点赞
收藏
分享

微信扫一扫

Java8 新特性02-方法引入


文章目录

  • ​​方法引入​​
  • ​​什么是方法​​
  • ​​方法引用​​
  • ​​MayiktService​​
  • ​​MessageInterface​​
  • ​​MessageInterface2​​
  • ​​MessageInterface3​​
  • ​​静态方法引入​​
  • ​​实例对象引用​​
  • ​​构造函数引入​​
  • ​​对象方法引入​​

方法引入

什么是方法

方法引用:需要结合lambda表达式能够让代码变得更加精简。

  1. 匿名内部类使用
  2. Lambda调用匿名内部类
  3. 方法引入
方法引用
  1. 静态方法引入: 类名::(静态)方法名称
  2. 对象方法引入 类名:: 实例方法名称
  3. 实例方法引入 new对象 对象实例::方法引入
  4. 构造函数引入 类名::new

需要遵循一个规范:

方法引入 方法参数列表、返回类型与函数接口参数列表与返回类型必须要保持一致

类型

语法

对应lambda表达式

构造器引用

Class::new

(args) -> new 类名(args)

静态方法引用

Class::static_method

(args) -> 类名.static_method(args)

对象方法引用

Class::method

(inst,args) -> 类名.method(args)

实例方法引用

instance::method

(args) -> instance.method(args)

方法引用提供了非常有用的语法, 可以直接引用已有的Java类或对象的方法或构造器, 方法引用其实也离不开lambda表达式

当lambda联合使用.减少代码的冗余

方法引用提供非常有用的语法 ,可以直接引用已有的Java类或者对象中方法或者构造函数方法引用需要配合lambda表达式语法一起使用减少代码的冗余性问题

  1. 构造器引用
  2. 静态引用
  3. 对象方法引用
  4. 实例方法引用
MayiktService

public interface MayiktService {
String get(Test23 test23);
}

MessageInterface

@FunctionalInterface
public interface MessageInterface {
void get(Integer var1, Integer var2);
}

MessageInterface2

@FunctionalInterface
public interface MessageInterface2 {
String getMessage();
}

MessageInterface3

@FunctionalInterface
public interface MessageInterface3 {
MessageEntity getMessage();
}

静态方法引入

public class TestLL01 {
public static void main(String[] args) {
//最原生的调用
// new MessageInterface() {
// @Override
// public String get(Integer var1, Integer var2) {
// System.out.println(ssss);;
// }
// }
//方法引入
MessageInterface staticGet = TestLL01::staticGet;
staticGet.get(12, 20);
}
public static void staticGet(Integer a, Integer b) {
System.out.println("staticGet,a:" + a + "+++" + b);
}
}

实例对象引用

public class TestLL02 {
/**
* 实例对象引用
*
* @param args
*/
public static void main(String[] args) {
// MessageInterface2 messageInterface2 = () -> {
// return "dasdasd";
// };
// System.out.println(messageInterface2.getMessage());
TestLL02 testLL02 = new TestLL02();
String object = testLL02.object();
System.out.println(object);
}

public String object() {
return "dadasda";
}
}

构造函数引入

public class TestLL03 {
/**
* 构造函数引用
*
* @param args
*/
public static void main(String[] args) {
MessageInterface3 messageInterface3 = () -> new MessageEntity();
AcanthopanaxInterface aNew = MessageEntity::new;
System.out.println(aNew);
}
}

对象方法引入

public class Test23 {
public static void main(String[] args) {
//1. 使用匿名内部类的形式
MayiktService mayiktService = new MayiktService() {
@Override
public String get(Test23 test23) {
return test23.object();
}
};
System.out.println(mayiktService.get(new Test23()));
//2.lambda
MayiktService mayiktService2 = test23 -> test23.object();
System.out.println(mayiktService2.get(new Test23()));

//3.方法引用 这哥时候函数接口 第一个参数传递test23 返回调用test23.object方法
MayiktService mayiktService3 = Test23::object;
System.out.println(mayiktService3.get(new Test23()));

// R apply(T t); T applu方法传递的参数类型 : R apply 方法返回的类型
//需要降string 类型字符串获取长度
// Function<String, Integer> stringIntegerFunction = (str) -> {
// return str.length();
// };
Function<String, Integer> stringIntegerFunction1 = String::length;
System.out.println(stringIntegerFunction1.apply("dasdadasd"));
}

private String object() {
return "niwwewe";
}
}


举报

相关推荐

0 条评论