Java 8中如何使用return args?

huangapple go评论96阅读模式
英文:

how return args works in Java 8?

问题

I'm new to Java8

Can anyone share what return args is returning in below code snippet? How it looks like in Java 7 to understand what's actually happening?

public ApplicationRunner initializeConnection(
RsvpsWebSocketHandler rsvpsWebSocketHandler) {
return args -> {
WebSocketClient rsvpsSocketClient = new StandardWebSocketClient();

  1. rsvpsSocketClient.doHandshake(
  2. rsvpsWebSocketHandler, MEETUP_RSVPS_ENDPOINT);
  3. };
  4. }
英文:

I'm new to Java8

Can anyone share what return args is returning in below code snippet ? How it looks like in Java 7 to understand what's actually happening ?

  1. public ApplicationRunner initializeConnection(
  2. RsvpsWebSocketHandler rsvpsWebSocketHandler) {
  3. return args -> {
  4. WebSocketClient rsvpsSocketClient = new StandardWebSocketClient();
  5. rsvpsSocketClient.doHandshake(
  6. rsvpsWebSocketHandler, MEETUP_RSVPS_ENDPOINT);
  7. };
  8. }

答案1

得分: 2

这是一个 lambda,只是匿名类的简写初始化。在 Java 1.7 中,它会像这样:

  1. return new ApplicationRunner() {
  2. @Override
  3. public void run(ApplicationArguments args) throws Exception {
  4. WebSocketClient rsvpsSocketClient = new StandardWebSocketClient();
  5. rsvpsSocketClient.doHandshake(rsvpsWebSocketHandler, MEETUP_RSVPS_ENDPOINT);
  6. }
  7. };
英文:

It's a lambda, which is just a shorthand initialization of an anonymous class.
In Java 1.7 it would look like:

  1. return new ApplicationRunner() {
  2. @Override
  3. public void run(ApplicationArguments args) throws Exception {
  4. WebSocketClient rsvpsSocketClient = new StandardWebSocketClient();
  5. rsvpsSocketClient.doHandshake(rsvpsWebSocketHandler, MEETUP_RSVPS_ENDPOINT);
  6. }
  7. };

huangapple
  • 本文由 发表于 2020年8月14日 00:54:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/63399826.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定