@FunctionalInterface // 明确是一个函数式接口
interface IMessage{
public void send(String str); // 只有一个接口,这种接口被称为函数式接口
}
public class lamdademo1 {
public static void main(String[] args) {
IMessage msg = new IMessage() {
@Override
public void send(String str) {
System.out.println(str);
}
};
msg.send("www.hello.com");
}
}
@FunctionalInterface // 明确是一个函数式接口
interface IMessage{
public void send(String str); // 只有一个接口,这种接口被称为函数式接口
}
// 使用lamda表达式实现与以上一样的功能,lamda要想使用,必须有一个重要的实现要求,只有一个抽象方法,只有函数式接口才能使用lamda表达式。
// lamda格式:
// 无参数:()->{}
// 有参数:(参数,参数) - > {}
// 如果只有一行语句返回:(参数,参数) -> {}
public class lamdademo1 {
public static void main(String[] args) {
IMessage msg = (str)-> {
System.out.println(str);
};
msg.send("www.hello.com");
}
}
// 如果只有一行语句返回:(参数,参数) -> {}
interface IMath{
public int add(int x, int y);
}
public class lamdademo1{
public static void main(String[] args) {
IMath math = (t1, t2) ->{
return t1 + t2;
};
System.out.println(math.add(12,33));
}
}
//优化上面的实现,eg: (t1, t2) -> t1 +t2;
interface IMath{
public int add(int x, int y);
}
// 利用lamda表达式,可以使用表达式更加方便。
public class lamdademo1{
public static void main(String[] args) {
IMath math = (t1, t2) -> t1 +t2;
System.out.println(math.add(12,33));
}
}