package jdk18.method.reference;
public class Student {
private String name;
private int score;
public Student(){
}
public Student(String name,int score){
this.name = name;
this.score = score;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
public static int compareStudentByScore(Student student1,Student student2){
return student1.getScore() - student2.getScore();
}
public int compare1(Student other){
return this.score - other.score;
}
public int compare2(Student student1,Student student2){
return student1.getScore() - student2.getScore();
}
public void print(){
System.out.print(this);
}
public Integer aa(int i){
System.out.println(score+i);
return i;
}
public void bbb(Integer i){
System.out.println(i);
}
public static void ccc(int i){
System.out.println(i);
}
@Override
public String toString() {
return "Student [name=" + name + ", score=" + score + "]";
}
}
package jdk18.method.reference;
public class StudentComparator {
public int compareStudentByScore(Student student1,Student student2){
return student2.getScore() - student1.getScore();
}
}
package jdk18.method.reference;
public interface TestInterface {
public int t(Student t1, Student t2, Student t3);
}
测试类;
package jdk18.method.reference;
import java.util.Arrays;
import java.util.List;
import java.util.function.BiFunction;
public class TestMain {
public static void main(String[] args) {
f5();
}
static List<Student> students = null;
static {
Student student1 = new Student("zhangsan",60);
Student student2 = new Student("lisi",70);
Student student3 = new Student("wangwu",80);
Student student4 = new Student("zhaoliu",90);
students = Arrays.asList(student1,student2,student3,student4);
}
static void f1(){
students.sort((o1, o2) -> o1.getScore() - o2.getScore());
students.forEach(student -> System.out.println(student.getScore()));
}
static void f2(){
students.sort(Student::compareStudentByScore);
students.forEach(student -> System.out.println(student.getScore()));
}
static void f3(){
StudentComparator studentComparator = new StudentComparator();
students.sort(studentComparator::compareStudentByScore);
students.forEach(student -> System.out.println(student.getScore()));
}
static void f4(){
students.forEach(Student::print);
}
/**
* 实例方法引用总结:首先肯定是与函数式接口强相关,
* 类名::方法名,其实感觉是this隐式的作为参数传入方法内,调用的时候直接用this调用这个方法,
* 所以,这种情况方法引用的参数要比函数式接口中的抽象方法少一个,即this占用了一个,而且是占用的第一个,
* 如果函数式接口的抽象方法有一个参数, 1.可以直接调用无参数方法(this表示该参数,引用的方法,即直接使用这个this来调用)
*
* 如果函数式接口的抽象方法有两个参数 2.只能引用有一个参数的方法,且该参数类型必须是这个类的类型,相当于改函数式接口中的第二个参数,
* 因为第一个参数还是被this占用着。
* 注:集合也会自动帮助顺序取多个元素与参数方法的多个参数相对应
*/
static void f5(){
students.sort(Student::compare1);
students.forEach(System.out::print);
//students.forEach(Student::compare1);
//students.sort(Student::compare2);
//TestMain.filter(Student::compare2);
}
public static void hehe(BiFunction<Student, Integer, Integer>on){
System.out.println(on.apply(students.get(0), 3));
}
public static void f6(){
BiFunction<Student, Integer, Integer> on = Student::aa;
hehe(on);
}
public static void f7(){
BiConsumer<Student, Integer> a = Student::bbb;
}
public static void f7dot5(){
Student b = new Student();
Consumer<Integer> a = b::bbb;
}
public static void f8(){
Consumer<Integer> a = Student::ccc;
}
static void filter(TestInterface i){
if(i.t(students.get(0), students.get(0), students.get(0)) == 0){
System.out.println("oooooooooo");
}
}
}