FashionCodeBoy 2016-06-08 03:11 采纳率: 100%
浏览 3312
已采纳

关于Lambda表达式的报错问题?

package Against;

import Object.LambdaQs;

interface Eatable
{
void taste();
}
interface Flyable
{
void fly(String weather);
}
interface Addable
{
int add(int a,int b);
}
public class LambdaQS {
//调用该方法需要Eatable对象
public void eat(Eatable e)
{
System.out.println(e);
e.taste();
}
//调用该方法需要Flyable对象
public void drive(Flyable f)
{
System.out.println("我正在驾驶:"+f);
f.fly("清空如洗的晴日");
}
//调用该方法需要Addable对象
public void test(Addable add)
{
System.out.println("5与3的和为"+add.add(5, 3));
}
public static void main(String[] args)
{
LambdaQs lq=new LambdaQs();
//Lambda表达式的代码块只有一条语句,可以省略花括号
lq.eat(()-> System.out.println("苹果的味道不错!"));
//lambda表达式的形参列表只有一个形参是,可以省略圆括号
lq.drive(weather->
{
System.out.println("今天天气是:"+weather);
System.out.println("直升机飞行平稳");
});
//代码块只有一条语句的时候,可以省略花括号,如果代码块中只有一条语句,几十该表达式需要返回值,也可以省略return关键字

    lq.test((a,b)->a+b);
}

}
Eclipse:
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
The type Eatable from the descriptor computed for the target context is not visible here.

The method drive(Flyable) in the type LambdaQs is not applicable for the arguments (( weather) -> {})
The type Flyable from the descriptor computed for the target context is not visible here.

The method test(Addable) in the type LambdaQs is not applicable for the arguments (( a, b) -> {})
The type Addable from the descriptor computed for the target context is not visible here.

at Against.LambdaQS.main(LambdaQS.java:39)
我用的JDK是支持Java8的
  • 写回答

2条回答 默认 最新

  • 丁垠午 2016-06-08 04:32
    关注

    你的代码并没有什么问题。已测,能正常运行。看看环境对不对吧

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题