有一个类,用了单例模式:用内部类实现static初始化。
这个类同时实现了一个接口。
接口声明的时候,调用static 的get实例的方法。
问题:其中有一个普通方法很奇怪,加上@override注解的时候,这个类就报错了,不能访问到内部类。 但是去掉这个注解就好了。 请问这是什么原因呢?
补充:
接口是有定义的哦,这个类报的错不能访问到内部类,运行时候报的错,编译没有问题。
附上代码
import net.sf.json.JSONObject;
public interface IRUtil {
public String excuteR(String content);
public JSONObject excuteR(String areaId,String content);
}
import net.sf.json.JSONObject;
import org.rosuda.REngine.Rserve.RConnection;
import org.rosuda.REngine.Rserve.RserveException;
public class DemandTypeRUtil implements IRUtil{
private RConnection connection = null;
private DemandTypeRUtil() {
System.out.println("执行R脚本-预测长短流程");
try {
connection = new RConnection("132.228.97.46");
// 创建对象
connection.setStringEncoding("utf8");// 设置字符编码
connection.eval("source('/usr/RFile/demandType.R')");
} catch (RserveException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
// 不关闭连接,加载R文件需要时间,一次加载一直使用。
// connection.close();
}
}
private static class DemandTypeRUtilHolder {
private static DemandTypeRUtil instance = new DemandTypeRUtil();
}
public static DemandTypeRUtil getInstance() {
return DemandTypeRUtilHolder.instance;
}
public synchronized String excuteR(String content) {
String result = "";
try {
result = connection.eval("findDemandType(\"" + content + "\")")
.asString();
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
public synchronized JSONObject excuteR(String areaId, String content) {
// TODO Auto-generated method stub
return new JSONObject();
}
}
public class RUtilService {
public static IRUtil getDemandTypeRUtil(){
IRUtil rUtil = DemandTypeRUtil.getInstance();
return rUtil;
}
}
public class RTest {
public static void main(String[] args) throws Exception{
IRUtil iRUtil = RUtilService.getDemandTypeRUtil();
String result = iRUtil.excuteR("12");
}
}
DemandTypeRUtil 类里的excuteR 上面加上@override就不行,运行时候报错, 这个例子是R语言调用的。 去掉R相关的语句应该没啥影响。