课程: Java: Advanced Concepts for High-Performance Development

今天就学习课程吧!

今天就开通帐号,24,700 门业界名师课程任您挑!

Implementing lambdas in Java

Implementing lambdas in Java

- Now that we've seen how functional interfaces work, let's see how we can replace a lot of the boiler plate code by using landers instead. So, so far I've got two implementations of my greeting interface. One called Hello World Greeting, and one called Good Morning Greeting. So, let's say I want to add a third one that prints out good afternoon. If I did that the same way as the others, I'd need to create a new class called Good Afternoon Greeting. But instead I'm going to do this using a lander. So, I'm going to add a new variable in my method, and it's going to be a type greeting, which is the interface name, and I'm going to call this one Good Afternoon Greeting. Then after the equal sign, I'm going to write something which is called an anonymous function. Basically, I'm going to implement the print message method, but using a special syntax. So the first thing I put after the equal sign is a pair of brackets. If…

内容