数据库我恨你 2023-03-26 01:10 采纳率: 100%
浏览 40
已结题

eclipse编译测试类出错

img

一直显示这个 解决不了了 谢谢各位!
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
No enclosing instance of type Shape is accessible. Must qualify the allocation with an enclosing instance of type Shape (e.g. x.new A() where x is an instance of Shape).
No enclosing instance of type Shape is accessible. Must qualify the allocation with an enclosing instance of type Shape (e.g. x.new A() where x is an instance of Shape).



  • 写回答

3条回答 默认 最新

  • 海洋 之心 2022年度博客之星人工智能领域TOP 1 2023-03-26 09:56
    关注

    这个错误通常是因为在创建内部类的实例时,没有提供外部类的实例。在 Java 中,内部类可以访问外部类的成员,但是在创建内部类的实例时,需要提供外部类的实例作为参数,否则无法访问外部类的成员变量和方法。

    在错误消息中,可能是在创建内部类的实例时出现了问题。具体解决方法取决于代码中的具体情况,以下是一些可能的解决方案:

    1. 创建外部类的实例并传递给内部类

    如果内部类需要访问外部类的成员变量或方法,需要创建外部类的实例,并将其传递给内部类的构造函数。例如:

    public class Shape {
        private int x;
        private int y;
    
        public class Point {
            public Point(int x, int y) {
                Shape.this.x = x; // 通过外部类实例访问外部类成员变量
                this.y = y; // 访问内部类成员变量
            }
        }
    }
    

    在创建 Point 类的实例时,需要提供外部类的实例:

    Shape shape = new Shape();
    Shape.Point point = shape.new Point(10, 20);
    
    1. 将内部类声明为静态类

    如果内部类不需要访问外部类的成员变量或方法,可以将其声明为静态类。静态类与外部类没有绑定关系,可以直接创建实例。例如:

    public class Shape {
        private int x;
        private int y;
    
        public static class Point {
            private int x;
            private int y;
    
            public Point(int x, int y) {
                this.x = x;
                this.y = y;
            }
        }
    }
    

    在创建 Point 类的实例时,不需要提供外部类的实例:

    Shape.Point point = new Shape.Point(10, 20);
    

    以上是两种可能的解决方案,具体方法取决于代码中的具体情况。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 4月5日
  • 已采纳回答 3月28日
  • 创建了问题 3月26日

悬赏问题

  • ¥15 r语言神经网络自变量重要性分析
  • ¥15 基于双目测规则物体尺寸
  • ¥15 wegame打不开英雄联盟
  • ¥15 公司的电脑,win10系统自带远程协助,访问家里个人电脑,提示出现内部错误,各种常规的设置都已经尝试,感觉公司对此功能进行了限制(我们是集团公司)
  • ¥15 救!ENVI5.6深度学习初始化模型报错怎么办?
  • ¥30 eclipse开启服务后,网页无法打开
  • ¥30 雷达辐射源信号参考模型
  • ¥15 html+css+js如何实现这样子的效果?
  • ¥15 STM32单片机自主设计
  • ¥15 如何在node.js中或者java中给wav格式的音频编码成sil格式呢