lamdba引用类对象的实例方法问题

Z灬Yao 2016-08-16 03:22:09
@FunctionalInterface
interface Test2 {
String test2(String a);
}

public class TestInterface implements TestI {

public TestInterface() {
// TODO Auto-generated constructor stub
System.out.println("TestInterface构造方法");
}
String Test3(String tem){
System.out.println(tem);
return "te";
}


public static void main(String[] args) {
Test2 Tes2 = TestInterface::Test3;
}


以上,主函数是报错的,内容为:
Cannot make a static reference to the non-static method Test3(String) from the type TestInterface
...全文
417 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
58coding 2016-08-29
  • 打赏
  • 举报
回复
有关Java8 Lambda表达式可以看下这篇文章,http://58coding.com/article/detail/24656434620795220
Z灬Yao 2016-08-17
  • 打赏
  • 举报
回复
书里这节说的是调用类的实例方法啊
Z灬Yao 2016-08-17
  • 打赏
  • 举报
回复
那有一句
MyTest mt2 = String::substring;
是没有问题的 这个String Substring 是静态方法么
soton_dolphin 2016-08-17
  • 打赏
  • 举报
回复
第一个方法就是把 Test3 变作static 方法 第二个方法就是 实例化TestInterface, 然后用他的这个实例去调用Test3. TestInterface tf = new TestInterface() Test2 Tes2 = tf::Test3;
  • 打赏
  • 举报
回复
翻译一下那句话
tatakautsubasa 2016-08-17
  • 打赏
  • 举报
回复
看报错内容,是说Test3方法非静态。你把Test3变为static试试。
soton_dolphin 2016-08-17
  • 打赏
  • 举报
回复
引用 5 楼 qq_29785973 的回复:
那有一句
MyTest mt2 = String::substring;
是没有问题的 这个String Substring 是静态方法么
你程序的前后文都不贴全了,就给一句代码出来,怎么帮你解决问题?
Z灬Yao 2016-08-16
  • 打赏
  • 举报
回复
求问为什么啊

62,621

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧