Java 8中如何使用return args?

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

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();

           rsvpsSocketClient.doHandshake(
               rsvpsWebSocketHandler, MEETUP_RSVPS_ENDPOINT);           
       };
   }
英文:

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();

               rsvpsSocketClient.doHandshake(
                   rsvpsWebSocketHandler, MEETUP_RSVPS_ENDPOINT);           
           };
       }

答案1

得分: 2

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

return new ApplicationRunner() {
    @Override
    public void run(ApplicationArguments args) throws Exception {
        WebSocketClient rsvpsSocketClient = new StandardWebSocketClient();

        rsvpsSocketClient.doHandshake(rsvpsWebSocketHandler, MEETUP_RSVPS_ENDPOINT);
    }
};
英文:

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

return new ApplicationRunner() {
            @Override
            public void run(ApplicationArguments args) throws Exception {
                WebSocketClient rsvpsSocketClient = new StandardWebSocketClient();

                rsvpsSocketClient.doHandshake(rsvpsWebSocketHandler, MEETUP_RSVPS_ENDPOINT);
            }
        };

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:

确定