数据库我恨你 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日

悬赏问题

  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来