Java8特性__方法引用与构造器引用

方法引用:


1. 举例:
Integer :: compare;

2. 方法引用的理解
> 方法引用,可以看做是基于lambda表达式的进一步刻画。
> 当需要提供一个函数式接口的实例时,我们可以使用lambda表达式提供此实例。
    > 当满足一定的条件的情况下,我们还可以使用方法引用或构造器引用替换lambda表达式。

3. 方法引用的本质:
方法引用作为了函数式接口的实例。  ---> “万事万物皆对象”

4. 格式:
类(或对象) :: 方法名

5. 具体使用情况说明:
情况1:对象 :: 实例方法

要求:函数式接口中的抽象方法a与其内部实现时调用的对象的某个方法b的形参列表和返回值类型都相同(或一致)。
此时,可以考虑使用方法b实现对方法a的替换、覆盖。此替换或覆盖即为方法引用。

注意:此方法b是非静态的方法,需要对象调用。


public class Employee {

	private int id;
	private String name;
	private int age;
	private double salary;

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public double getSalary() {
		return salary;
	}

	public void setSalary(double salary) {
		this.salary = salary;
	}

	public Employee() {
		System.out.println("Employee().....");
	}

	public Employee(int id) {
		this.id = id;
		System.out.println("Employee(int id).....");
	}

	public Employee(int id, String name) {
		this.id = id;
		this.name = name;
	}

	public Employee(int id, String name, int age, double salary) {

		this.id = id;
		this.name = name;
		this.age = age;
		this.salary = salary;
	}

	@Override
	public String toString() {
		return "Employee{" + "id=" + id + ", name='" + name + '\'' + ", age=" + age + ", salary=" + salary + '}';
	}

}
 
        Employee emp = new Employee(1001,"wuweixian",34,60000);
        Supplier sup1 = emp :: getName;
        System.out.println(sup1.get());


情况2:类 :: 静态方法

要求:函数式接口中的抽象方法a与其内部实现时调用的类的某个静态方法b的形参列表和返回值类型都相同(或一致)。
此时,可以考虑使用方法b实现对方法a的替换、覆盖。此替换或覆盖即为方法引用。

注意:此方法b是静态的方法,需要类调用。

 Comparator<Integer> com = new Comparator<Integer>() {
            @Override
            public int compare(Integer t1, Integer t2) {
              return Integer.compare(t1,t2);
            }
        };
        System.out.println(com.compare(12,21));

        System.out.println("*******************");
        Comparator<Integer> com1 = (t1,t2) -> Integer.compare(t1,t2);
        System.out.println(com1.compare(12,21));

        System.out.println("*******************");
        Comparator<Integer> com2 = Integer::compare;
        System.out.println(com2.compare(12,3));


情况3: 类 :: 实例方法

要求:函数式接口中的抽象方法a与其内部实现时调用的对象的某个方法b的返回值类型相同。
    同时,抽象方法a中有n个参数,方法b中有n-1个参数,且抽象方法a的第1个参数作为方法b的调用者,且抽象方法a
    的后n-1个参数与方法b的n-1个参数的类型相同(或一致)。则可以考虑使用方法b实现对方法a的替换、覆盖。此替换或覆盖即为方法引用。

注意:此方法b是非静态的方法,需要对象调用。但是形式上,写出对象a所属的类 (笔误,改为:写成对象所属的类)

//初始 
Comparator<String> com = new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                return o1.compareTo(o2);
            }
        };
        System.out.println(com.compare("12", "21"));
        System.out.println("************************************");
//lambda表达式
        Comparator<String> com1 = (o1,o2) -> o1.compareTo(o2);
        System.out.println(com1.compare("12","21"));
        System.out.println("************************************");
//方法引用:为类型调用实例方法的例子
        Comparator<String> com3 = String::compareTo;
        System.out.println(com3.compare("122", "122"));

构造器引用:


1.1 格式:
类名 :: new

1.2 说明:
> 调用了类名对应的类中的某一个确定的构造器
> 具体调用的是类中的哪一个构造器呢?取决于函数式接口的抽象方法的形参列表!

 //1.
        Supplier<Employee> sup = new Supplier<Employee>() {
            @Override
            public Employee get() {
                return new Employee();
            }
        };
        System.out.println(sup.get());
        //2.
        Supplier<Employee> sup1 =() -> new Employee();
        System.out.println(sup1.get());
        //33
        Supplier<Employee> sup2 = Employee::new;
        System.out.println(sup2.get());

2. 数组引用
格式:数组名[] :: new

 //1.
Function<Integer,Employee[]> func1 = new Function<Integer, Employee[]>() {
            @Override
            public Employee[] apply(Integer length) {
                return new Employee[length];
            }
        };
        //2.
        Function<Integer,Employee[]> func2 = length -> new Employee[length];
        //3.
        Function<Integer,Employee[]> func3 = Employee[] :: new;


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值