Created
February 1, 2018 13:40
-
-
Save Summerlve/69c46e5b6ee24f12f0fe03a54b557169 to your computer and use it in GitHub Desktop.
DoSomething.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// (x,y)->Math.pow(x,y)其实等价于一个匿名内部类,你传入一个lambda其实是传了一个匿名内部类对象。 | |
// 比如有一个类库代码如下: | |
public class DoSomething { | |
private int x; | |
private int y; | |
public int pow(Math m) { | |
return m.pow(this.x, this.y); | |
} | |
public DoSomething setX(int x) { | |
this.x = x; | |
return this; // 返回对象自身,以便于链式调用。 | |
} | |
public DoSomething setY(int y) { | |
this.y = y; | |
return this; // 返回对象自身,以便于链式调用。 | |
} | |
} | |
你在使用该类库方法时可能这么使用: | |
DoSomething do = new DoSomething(); | |
do.setX(1).setY(2).pow(Math::pow); // 此行返回m.pow(this.x, this.y);的结果 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment