sun_ny_fu 2017-06-06 09:02 采纳率: 0%
浏览 897

请教个内部类实现的单例模式,访问不到内部类的问题。

有一个类,用了单例模式:用内部类实现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相关的语句应该没啥影响。

  • 写回答

4条回答 默认 最新

  • Hefei19881002 2017-06-06 09:24
    关注

    把你代码贴出来 才好看

    评论

报告相同问题?

悬赏问题

  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊