基本介绍
public class NewDemo {
public static void printData(NewInterface inter) {
inter.print("HelloWorld");
}
public static void main(String[] args) {
printData(/*s->{
System.out.println(s);
}*/System.out::println); //system.out引用println方法
}
}
通过对象名引用成员方法
public interface MethodCite1 {
public abstract void print(String str);
}
public class MethodObj {
public void method(String str) {
System.out.println(str.toUpperCase());
}
}
public class TheTotalClass {
public static void method01(MethodCite1 mc1) {
mc1.print("Hello");
}
public static void main(String[] args) {
method01((s)->{
MethodObj newObj = new MethodObj();
newObj.method(s);
});
}
}
其实就等价于
public class NewObj {
public static void method01(MethodCite1 mc1) {
mc1.print("Hello");
}
public static void main(String[] args) {
method01(new MethodCite1() {
@Override
public void print(String str) { //匿名内部类重写接口的抽象方法
MethodObj newObj = new MethodObj();
newObj.method(str);
}
});
}
}
虽然觉得确实可以省略代码,但是总感觉阅读起来没那么流畅。。。
通过类名引用静态成员方法
public class TheTotalClass {
public static int method01(int number, MethodCite1 mc1) {
return mc1.printAbs(number);
}
public static void main(String[] args) {
/*int number = method01(-10,(n)->{
return Math.abs(n);
});*/
int number = method01(-10,Math::abs);
System.out.println(number);
}
}
通过super引用父类的成员方法
@FunctionalInterface
public interface MethodCite1 {
public abstract void newMethod();
}
public class MethodObj {
public void method01() {
System.out.println("我是父类");
}
}
public class SecondClass extends MethodObj{
public void method01() {
System.out.println("我是子类");
}
public void method02(MethodCite1 mc1) {
mc1.newMethod();
}
public void show() {
/*method02(()->{
MethodObj meOb = new MethodObj();
meOb.method01();
});*/
/*method02(()->{
super.method01();
});*/
method02(super::method01);
}
public static void main(String[] args) {
new SecondClass().show();
}
}
通过this引用本类的成员方法
@FunctionalInterface
public interface MethodCite1 {
public abstract void newMethod();
}
public class SecondClass{
public void method01() {
System.out.println("我是子类");
}
public void method02(MethodCite1 mc1) {
mc1.newMethod();
}
public void show() {
/*method02(()->{
this.method01();
});*/
method02(this::method01);
}
public static void main(String[] args) {
new SecondClass().show();
}
}
类的构造器引用
public class Person {
private String name;
public Person(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
@FunctionalInterface
public interface NewInterface {
Person newPer(String name);
}
public class NewClass {
public static void personBuild(String name, NewInterface newIn) {
Person per = newIn.newPer(name);
System.out.println(per.getName());
}
public static void main(String[] args) {
/*personBuild("小新",(name)->{
return new Person(name);
});*/
personBuild("小新",Person::new);
}
}
数组的构造器引用
@FunctionalInterface
public interface NewArr {
int[] arrBuilder(int length);
}
public class TestDemo {
public static int[] method(int length, NewArr nArr) {
return nArr.arrBuilder(length);
}
public static void main(String[] args) {
/*int[] ints = method(10,(int length)->{
return new int[length];
});*/
int[] ints = method(10,int[]::new);
int length = ints.length;
System.out.println(length);
}
}