Java 没有可访问的封闭类型实例

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18690770/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 10:13:05  来源:igfitidea点击:

No enclosing instance of type is accessible

java

提问by user1896464

I wrote this Java interface program in Eclipse, but there is a red line under MyTriangle tmp = new MyTriangle();and when i run the program I get this error:

我在Eclipse中写了这个Java接口程序,但是MyTriangle tmp = new MyTriangle();下面有一条红线当我运行程序时,出现此错误:

No enclosing instance of type Question1 is accessible. Must qualify the allocation with an enclosing instance of type Question1 (e.g. x.new A() where x is an instance of Question1).

无法访问类型为 Question1 的封闭实例。必须使用类型为 Question1 的封闭实例限定分配(例如,xnew A(),其中 x 是 Question1 的实例)。

 public static void main(String[] args) 
    {   
     MyTriangle tmp = new MyTriangle();
     tmp.getSides();
     System.out.println();
     System.out.println("The area of the triangle is " + tmp.computeArea());
     }

interface Triangle
{
 public void triangle();
 public void iniTriangle(int side1, int side2, int side3);
 public void setSides(int side1, int side2, int side3);
 public void getSides();
 public String typeOfTriangle(); 
 public double computeArea();            
}

 class MyTriangle implements Triangle
 {
  private int side1,side2,side3;
  public  void triangle()
  {
    this.side1 = 3;
    this.side2 = 4;
    this.side3 = 5;
  } 
}

采纳答案by upog

Try this. Removed the methods for simplicity

尝试这个。为简单起见删除了方法

public class Test1 {     

    public static void main( String [] args) 
    { 
        MyTriangle h1 = new MyTriangle();     
    } 
} 
class MyTriangle implements Triangle{
    int side1;
    int side2;
    int side3;

    public MyTriangle(){
        this.side1 = 1;
        this.side2 = 2;
        this.side3 = 3;
    }
}
interface Triangle{}


You have not pasted your full code, I assume your code should look something like below.

您尚未粘贴完整代码,我认为您的代码应如下所示。

Then you should create the Instance for your main class before creating instance for your triangle as shown below

然后你应该在为你的三角形创建实例之前为你的主类创建实例,如下所示

public class Test{
     class MyTriangle 
     {
      int side1,side2,side3;
      public   MyTriangle()
      {
        this.side1 = 3;
        this.side2 = 4;
        this.side3 = 5;
      } 

    }
public static void main(String[] args) 
    {   
     MyTriangle h1 = new Test(). new MyTriangle();   // Fix is here**   
     }
}

interface Triangle{}

回答by Ravi Thapliyal

MyTriangleis a non-static inner class. That means like all other instance members it (& it's instance) belongs to an instance of the outer class as opposed to the class itself. Remember to belong to a class things need to be defined as static.

MyTriangle是一个非静态内部类。这意味着与所有其他实例成员一样,它(& 它的实例)属于外部类的实例,而不是类本身。记住属于一个类的东西需要定义为static.

Hence, you need to provide an outer class instance to instantiate the inner one as

因此,您需要提供一个外部类实例来将内部类实例化为

new OuterClass().new MyTriangle();

If you mark the inner class staticwhich makes it nestedit would allow you to refer to it in a static context like a public static main()method.

如果您标记static使其嵌套的内部类,它将允许您在静态上下文中引用它,如公共静态main()方法。