Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/321.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
返回语句中对象的Java lambda类型转换?_Java_Lambda_Spring Cloud Dataflow - Fatal编程技术网

返回语句中对象的Java lambda类型转换?

返回语句中对象的Java lambda类型转换?,java,lambda,spring-cloud-dataflow,Java,Lambda,Spring Cloud Dataflow,我的spring云应用程序中有以下代码: @EnableBinding(Source.class) @SpringBootApplication public class dataflow_producer { static SimpleDateFormat dateFormatter = new SimpleDateFormat("hh:mm:ss"); public static void main(String[] args) { SpringApplication.run(


我的spring云应用程序中有以下代码:

@EnableBinding(Source.class)
@SpringBootApplication
public class dataflow_producer {

static SimpleDateFormat dateFormatter = new SimpleDateFormat("hh:mm:ss"); 

public static void main(String[] args) {
    SpringApplication.run(dataflow_producer.class, args);

}

@Bean
@InboundChannelAdapter(value = Source.OUTPUT)
public MessageSource<String> timerMessageSource() {       
    return ()-> new GenericMessage<String>(dateFormatter.format(new Date()));
}
}
@EnableBinding(Source.class)
@SpringBoot应用程序
公共类数据流\u生成器{
静态SimpleDataFormat dateFormatter=新SimpleDataFormat(“hh:mm:ss”);
公共静态void main(字符串[]args){
run(dataflow_producer.class,args);
}
@豆子
@InboundChannelAdapter(值=Source.OUTPUT)
public MessageSource timerMessageSource(){
return()->newgenericmessage(dateFormatter.format(newdate());
}
}
我的问题是,这句话是怎么说的

return ()-> new GenericMessage<String>(dateFormatter.format(new Date()));
return()->newgenericmessage(dateFormatter.format(newdate());
它是否像这样重新键入object

return (MessageSource<String>)new GenericMessage<String>(dateFormatter.format(new Date()));
return(MessageSource)new GenericMessage(dateFormatter.format(new Date());
你能解释一下
return()->
的含义吗?为什么在这种情况下它是有效的?输入是
GenericMessage
类型的
Object
,但输出是
MessageSource
类型的
Object

提到的
return
语句可以重写为非lambda代码吗?

我不知道Spring

但是,在Java8中。当您获得SAM接口(单一抽象方法)时。例如调用“SAMInterface”

如果需要返回“SAMInterface”类型的对象,则以下代码等效:

SAMInterface toReturn = () -> println("Hello World!");

所以如果
MessageSource
接口正好有一个方法需要重新定义。
您可以使用lambda:)

就是这样!我查看了文档,MessageSource实际上只有一个方法“receive”。所以我通过实现这个方法创建了MessageSource的新实例。IDE警告和运行时ClassCastException错误消失。非常感谢!
SAMInterface toReturn = () -> println("Hello World!");
SAMInterface toReturn = new SAMInterface() {
    public void theSAMMethod() {
      println("Hello World!");
    }
}