我有一个主班,如下所示。我想将接口对象从Main方法传递给另一个方法。我正在通过下面的主课中所示的内容。但是我收到错误“;预期”。有人可以帮我吗?
这是我的主班:
package com.armus.web.server;
import com.armus.common.dtflow.DfService;
public class TriggerAgain
{
public static void main(String[] args){
Long i= 97944605L;
DMSerImpl rdf = new DMSerImpl();
try {
/*********** Section start....Getting Problem In this section...How Can I Pass interface object to a method setDfService*********/
@Override
rdf.setDfService(new DfService()
{
Dfsessn dfsessn=getDfsessnById(i)
{
System.out.println("In main class...In interface method="+i);
}
})
/*************************************************Section End*******************************************************************/
com.armus.dto.Jinfo a=rdf.Trigger(i);
}
catch( Throwable e){
System.out.println("In exception = "+e+" "+i);
e.printStackTrace();
return; // Always must return something
}
}
}
这是我要将接口对象传递给“ setDfService”方法的另一个类。
package com.armus.web.server;
import com.armus.common.dtflow.DfService;
import com.armus.common.dto.Dfsessn;
import com.armus.foundation.client.exception.ServiceException;
import com.armus.dto.Jinfo;
public class DMSerImpl
extends Remoteservletsup
implements DMserv, DMAjxSer
{
private DfService dtflowser;
public void setDfService(DfService dtflowser)
{
this.dtflowser = dtflowser;
}
private Dfsessn getDfsessn(long sessionId)
throws ServiceException
{
try
{
dfSession = this.dtflowser.getDfsessnById(Long.valueOf(sessionId));
}
catch (ServerException e)
{
Dfsessn dfSession;
LOG.error(e.getMessage(), e);
throw new ServiceException(e.getMessage());
}
return dfSession;
}
public com.armus.dto.Jinfo Trigger(long sessionId)
throws ServiceException
{
Dfsessn dfSession = getDfsessn(sessionId);
//some code
}
}
下面是我的界面:
package com.armus.common.dtflow;
import com.armus.common.dto.Dfsessn;
import com.armus.common.exception.ServerException;
public abstract interface DfService
{
public abstract Dfsessn getDfsessnById(Long paramLong)
throws ServerException;
}
先谢谢您!!! :)
答案 0 :(得分:0)
这是创建匿名类实例的示例
rdf.setDfService(new DfService() {
@Override
public Dfsessn getDfsessnById(Long paramLong) {
....
}});
答案 1 :(得分:0)
您是:
@Override
放在错误的位置; 缺少分号:
rdf.setDfService(new DfService()
{
@Override
public Dfsessn getDfsessnById(Long i)
{
System.out.println("In main class...In interface method="+i);
}
});
答案 2 :(得分:-1)
@Override
rdf.setDfService(new DfService()
{
Dfsessn dfsessn=getDfsessnById(i)
{
System.out.println("In main class...In interface method="+i);
}
})
在您要执行的操作之上,是创建一个实现DfService
接口的匿名类实例,但您没有正确执行它(语法上明智)。
在https://www.geeksforgeeks.org/anonymous-inner-class-java/
处了解匿名内部类。此外,DfService
是一个功能接口(单方法接口),因此您也可以在此处使用lamda。像这样的东西。
rdf.setDfService(paramLong -> System.out.println("In main class...In interface method="+i));