怎么用vs做网站,别人怎么看见我做的网站,oa办公系统如何使用,网站建设与管理学什么1.方法引用
1.方法引用概述 注意注意#xff1a; 1.引用出必须是函数式接口 2.被引用的方法必须已经存在 3.被引用方法的型参和返回值需要跟抽象方法保持一致 4.被引方法的功能要满足当前需求 Arrays.sort(arr,Main::subtraction);
Main是该类的名称#xff0c;#xff1a… 1.方法引用
1.方法引用概述 注意注意 1.引用出必须是函数式接口 2.被引用的方法必须已经存在 3.被引用方法的型参和返回值需要跟抽象方法保持一致 4.被引方法的功能要满足当前需求 Arrays.sort(arr,Main::subtraction);
Main是该类的名称是方法引用符 //方法引用Integer[] arr{4,3,1,6,2,7,8,5};Arrays.sort(arr,run1::subtraction);System.out.println(Arrays.toString(arr));}public static int subtraction(int num1,int num2){return num2-num1;}} 2.引用静态方法 1.方法需要已经存在 2.方法的形参和返回值需要跟抽象方法的形参和返回值保持一致 3.方法的功能需要把形参的字符串转换成整数 未利用方法引用 方法引用如下引用方法parseInt ArrayListString list10 new ArrayList();Collections.addAll(list10, 1,4,3,2,6);list10.stream().map(Integer::parseInt).forEach(s-System.out.println(s)); 3.引用成员方法 格式对象成员方法 1.其他类其他类对象方法名 2.本类this方法名引用出处不能是父类方法 3.父类super方法名 1.引用其他成员方法 另一个类所定义的方法如/*线以下所示
第五行使用时候需要对象方法这里的对象是在另一个类所有需要new erer();
public class Main {public static void main(String[] args) {ArrayListString list1new ArrayList();Collections.addAll(list1,张ad,刘df,张ty,周kdh,fjd);list1.stream().filter(new erer()::stringpd).forEach(s - System.out.println(s));}}/* 另一个类的方法如下public class erer {public boolean stringpd(String s){return s.startsWith(张)s.length()3;}
}*/ 2.引用本类成员方法
注意静态方法无this(所以要用本类的对象new Main())
public class Main {public static void main(String[] args) {ArrayListString list1new ArrayList();Collections.addAll(list1,张ad,刘df,张ty,周kdh,fjd);list1.stream().filter(new Main()::stringpd).forEach(s - System.out.println(s));}}public class erer {public boolean stringpd(String s){return s.startsWith(张)s.length()3;}
} 3.引用父类成员方法
拓展挺难的 4.引用构造方法 稿子或者前提
未引用构造方法
//匿名内部类 ListStudent newlist1 list1.stream().map(new FunctionString, Student() {Overridepublic Student apply(String s) {String[] arr s.split(,);String name arr[0];int age Integer.parseInt(arr[1]);return new Student(name, age);}}).collect(Collectors.toList());//lambdaListStudent new2list1 list1.stream().map(s - new Student(s.split(,)[0], Integer.parseInt(s.split(,)[1]))).collect(Collectors.toList());
这里要修改Student的录入方式将String name,int age变为String strname和age在str中获得。 //引用构造方法ListStudent new3list1 list1.stream().map(Student::new).collect(Collectors.toList());System.out.println(new3list1);//其中Strudent这个类里面要加上public Student(String str) {String[] arr str.split(,);this.name arr[0];this.age Integer.parseInt(arr[1]);}
引用构造方法 5.其他调用方式
1.使用类名引用成员方式 格式类名成员方法 范例Stringsubstring 特有规则 所以下面才成立
注意第一个参数决定了我能引用那个类中的方法。如上图第一个参数是String引用的类是String。 2.引用数组的构造方式 格式数据类型[ ]new 范例int[ ]new 细节数组的类型需要跟流中数据的类型保持一致。 总结 6.实战演练
都挺难的
1.转成自定义对象并收集到数组
如下 Student[] arr的那一行中的第一个Studentnew是指引用构造方法Student[]是指引用数组的构造方法。 2.获取部分属性并收集到数组
如下 技巧 2.异常
1.