/**
* 静态方法引用
* @param <P> 引用方法的参数类型
* @param <R> 引用方法的返回类型
*/
@FunctionalInterface
interface FunStaticRef<P,R>{
public R tranTest(P p);
}
public static void main(String[] args) {
/*
* 静态方法引用: public static String valueOf
* 即将String的valueOf() 方法引用为 FunStaticRef#tranTest 方法
*/
FunStaticRef<Integer, String> funStaticRef = String::valueOf;
String str = funStaticRef.tranTest(10000);
System.out.println(str.replaceAll("0", "9"));
}
/**
* 普通方法引用
* @param <R> 引用方法返回类型
*/
@FunctionalInterface
interface InstanRef<R>{
public R upperCase();
}
public static void main(String[] args) {
/*
* 普通方法的引用: public String toUpperCase()
*
*/
String str2 = "i see you";
InstanRef<String> instanRef = str2 :: toUpperCase;
System.out.println(instanRef.upperCase());
}
/**
* 特定方法的引用
* @param <P>
*/
@FunctionalInterface
interface SpecificMethodRef<P>{
public int compare(P p1 , P p2);
}
public static void main(String[] args) {
/*
* 特定方法的引用 public int compareTo(String anotherString)
* 与之前相比,方法引用前不再需要定义对象,而是可以理解为将对象定义在了参数上!
*/
SpecificMethodRef<String> specificMethodRef = String :: compareTo;
System.out.println(specificMethodRef.compare("A","B"));
ConstructorRef<Book> constructorRef = Book :: new;
Book book = constructorRef.createObject("Java",100.25);
System.out.println(book);
}
class Book{
private String title;
private double price;
public Book() {
}
public Book(String title,double price){
this.price = price;
this.title = title;
}
@Override
public String toString() {
return "Book{" +"title='" + title + '\'' +", price=" + price +'}';
}
}
public static void main(String[] args) {
/*
* 构造方法引用
*/
ConstructorRef<Book> constructorRef = Book :: new;
Book book = constructorRef.createObject("Java",100.25);
System.out.println(book);
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有