iteye_16885 2011-06-10 11:25
浏览 244
已采纳

内部类可以在外部类的方法里实例化吗?如果可以那后面添加return后为什么有报错!

ServiceClass service = new ServiceClass(clienttype, requestXml);
new InsertThread(service).start();

提示错误信息如下:
No enclosing instance of type OrderServices is accessible. Must qualify the allocation with an enclosing instance of type OrderServices (e.g. x.new A()
where x is an instance of OrderServices).

内部类如下:
class ServiceClass{
private AuthenticatorTBEServiceStub atss = null;
private String clienttype;
private String requestXml;
public ServiceClass(String clienttype,String requestXml){
this.clienttype = clienttype;
this.requestXml = requestXml;
}

    public synchronized void insertService() throws RemoteException, ExceptionException{
        atss = new AuthenticatorTBEServiceStub();
        GetBookTicketE gbte = new GetBookTicketE();
        GetBookTicket gbt = new GetBookTicket();
        gbt.setClienttype(clienttype);
        gbt.setQcbag(requestXml);
        gbt.setQcresult(com.tempus.order.BookTicket.readBookXML(requestXml));
        gbte.setGetBookTicket(gbt);
        atss.getBookTicket(gbte);
    }

    public synchronized String responseXml(){
        return com.tempus.order.BookTicket.readBookXML(requestXml);
    }
}
class InsertThread extends Thread{
    private ServiceClass service;
    public InsertThread(ServiceClass service){
        this.service = service;
    }
    @Override
    public void run() {
        super.run();
        try {
            Thread.sleep(100);
            service.insertService();
        } catch (RemoteException e) {
            e.printStackTrace();
            Logs.info("抛出RemoteException,添加数据时错误!");
        } catch (ExceptionException e) {
            e.printStackTrace();
            Logs.info("抛出ExceptionException,添加数据时错误!");
        }catch (InterruptedException e) {               
            e.printStackTrace();
            Logs.info("抛出InterruptedException,添加数据时错误!");
        }
    }
}
class ResponseThread extends Thread{
    private ServiceClass service;
    public ResponseThread(ServiceClass service){
        this.service = service;
    }
    @Override
    public void run() {
        super.run();
        service.responseXml();
    }
}
  • 写回答

1条回答 默认 最新

  • 也许世界还没停 2011-06-10 12:30
    关注

    [code="java"]class Outter {
    class Inner {

    }
    

    }[/code]

    [code="java"] Outter out = new Outter();
    Inner in = out.new Inner();[/code]

    意思是需要这么实例化内部类

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘