Skip to content

Instantly share code, notes, and snippets.

@Summerlve
Created February 1, 2018 13:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Summerlve/69c46e5b6ee24f12f0fe03a54b557169 to your computer and use it in GitHub Desktop.
Save Summerlve/69c46e5b6ee24f12f0fe03a54b557169 to your computer and use it in GitHub Desktop.
DoSomething.java
// (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