标签:str this lint jdk8 creat alt from 调用 print
引用方法并运行
在Java中,方法和构造方法都看作是对象的一种,那么你要引用它(不是调用),则可以用::来引用。用来存储这个引用的类型用@FunctionlaInterface注解来标识。
示例:
package fun; /** * @author 施俊杰 * @email shijunjie.me@outlook.com */ public class TestMethods { @FunctionalInterface interface Fun<F1, F2, T> { T myrun(F1 from1, F2 from2); } public int add(int x, int y) { return x+y; } public static void main(String[] args) { Fun<Integer, Integer, Integer> f = new TestMethods()::add; int i = f.myrun(1, 2); System.out.println(i); System.out.println(f.toString()); } }
运行结果:
通过引用构造函数来创建对象
示例如下
package interfacetest; /** * @author 施俊杰 * @email shijunjie.me@outlook.com */ public class TestMethods2 { private int i; private int j; public TestMethods2(int i, int j) { this.i = i; this.j = j; } public int getI() { return i; } public void setI(int i) { this.i = i; } public int getJ() { return j; } public void setJ(int j) { this.j = j; } @Override public String toString() { return "TestMethods2 [i=" + i + ", j=" + j + "]"; } @FunctionalInterface interface constructor<F1, F2, T> { T create(F1 f1, F2 f2); } public static void main(String[] args) { constructor<Integer, Integer, TestMethods2> c = TestMethods2::new; TestMethods2 obj = c.create(1, 2); System.out.println(obj); } }
运行结果:
标签:str this lint jdk8 creat alt from 调用 print
原文地址:https://www.cnblogs.com/s648667069/p/8865395.html