简体   繁体   English

在作用域中无法访问类型为Polylinje的封闭实例

[英]no enclosing instance of the type Polylinje is accessible in scope

First of all, sorry for the Swedish in my code. 首先,对我的代码中的瑞典语表示抱歉。 It's a school assignment and they are written in Swedish... I hope the code is understandable. 这是学校的作业,他们用瑞典语编写...我希望代码易于理解。

I get this error on three lines in my code and have no idea why. 我在代码的三行中收到此错误,却不知道为什么。

no enclosing instance of the type Polylinje is accessible in scope

My code is: 我的代码是:

public class PolylinjeIterator { 
    private int aktuell = -1; 

    public PolylinjeIterator (){ 
        if (Polylinje.this.horn.length > 0) // ERROR HERE!
            aktuell = 0; 
    } 

    public boolean finnsHorn (){ 
        return aktuell != -1; 
    } 

    public Punkt horn () 
            throws java.util.NoSuchElementException{ 
        if (!this.finnsHorn ()) 
            throw new java.util.NoSuchElementException ( 
                    "slut av iterationen"); 

        Punkt horn = Polylinje.this.horn[aktuell]; // ERROR HERE!

        return horn; 
            } 

    public void gaFram (){ 
        if (aktuell >= 0 && 
                aktuell < Polylinje.this.horn.length - 1) // ERROR HERE!
            aktuell++; 
        else 
            aktuell = -1; 
    }
} 

the code inside Polylinje.java looks like this: Polylinje.java中的代码如下所示:

import java.util.Arrays;
public class Polylinje {

// Instansvariabler

// En tom Polylinje
private Punkt[] horn;

// Polylinjens färg
private String farg = "svart";

// Polylinjens bredd
private int bredd = 1;



// Konstruktorer

// Polylinje skapar en Polylinje utan hörn
public Polylinje () {
    this.horn = new Punkt[0];
}

// Polylinje skapar en Polylinje med argument
public Polylinje (Punkt[] horn, String farg, int bredd)
{
    this.horn = new Punkt[horn.length];
    for (int i = 0; i < horn.length; i++)
        this.horn[i] = new Punkt (horn[i]);

    this.farg = farg;
    this.bredd = bredd;
}
public Polylinje (Punkt[] horn)
{
    this.horn = new Punkt[horn.length];
    for (int i = 0; i < horn.length; i++)
        this.horn[i] = new Punkt (horn[i]);
}



// Konvertorer

//
public String toString () {
    String s = "";
    s = "{"+Arrays.toString(horn)+", "+farg+", "+bredd+"}";
    return s;
}



// Inspektorer

// getHorn returnerar hörnen i form av Punkt-array.
public Punkt[] getHorn () {return horn;}

// getFarg returnerar färgen i form av en String.
public String getFarg () {return farg;}

// getBredd returnerar bredden i form av en integer.
public int getBredd () {return bredd;}



// Mutatorer

// setFarg låter dig ange färgen på en Polylinje.
public void setFarg (String farg) {this.farg = farg;}

// setBredd låter dig ange bredden på en Polylinje.
public void setBredd (int bredd) {this.bredd = bredd;}

// langd beräknar längden på en Polylinje.
public double langd () {

    double langd = 0;
    double d = 0;

    for (int i = 0; i < (horn.length-1); i++){
        d = horn[i].avstand (horn[i+1]);
        langd += d;
    }

    return langd;
}

// laggTill lägger till en linje i slutet av Polylinjen
public void laggTill (Punkt horn) {
    Punkt[] h = new Punkt[this.horn.length + 1];
    int i = 0;
    for (i = 0; i < this.horn.length; i++)
        h[i] = this.horn[i];
    h[i] = new Punkt (horn);
    this.horn = h;
}

// laggTillFramfor lägger till en linje framför en vald linje
public void laggTillFramfor (Punkt horn, String hornNamn)
{
    int pos = -1;

    for(int i = 0; i < this.horn.length; i++){
      if(this.horn[i].namn == hornNamn){
         pos = i;
         break;
      }
    }

    Punkt[] h = new Punkt[this.horn.length + 1];

    for (int j = 0; j < pos; j++)
        h[j] = this.horn[j];


    for (int k = pos+1; k < h.length; k++)
        h[k] = this.horn[k-1];

    h[pos] = new Punkt (horn);

    this.horn = h;
}

//
public void taBort (String hornNamn) {}
}

If you want to refer to an instance of Polylinje inside PolylinjeIterator you will need to pass an instance of PolylinjeIterator to the constructor : 如果要在PolylinjeIteratorPolylinje PolylinjeIterator的实例,则需要将PolylinjeIterator的实例PolylinjeIterator给构造函数:

  public PolylinjeIterator (Polylinje polylinjeInstance){ 
        if (polylinjeInstance.horn().length > 0) // Assuming Punkt has a length member and horn is a method in Polylinje 
            aktuell = 0; 
    } 

If you want to use the Polylinje in different places in your PolylinjeIterator class create a class member and assign the given instance to this member in the constructor. 如果要在PolylinjeIterator类的不同位置使用Polylinje ,请创建一个类成员,并将给定实例分配给构造函数中的该成员。 Then use the member in your PolylinjeIterator class. 然后在PolylinjeIterator类中使用该成员。

Using Polylinje.this is meaningless since Classes do not have their own member as an instance. 使用Polylinje.this是没有意义的,因为Classes没有自己的成员作为实例。 The instance is what you create as a concrete entity of your class so whenever you refer to this the class name is not needed 实例为你所创造的作为你的类的具体实体,所以当你表示this是不需要的类名

The espression Polylinje.this.horn in your code is not valid. 您代码中的Polylinje.this.horn无效。 If you need to access a horn attribute in an instance of class Polylinje you need to make this instance accessible to class PolylinjeIterator , possibly by giving it an attribute of class Polylinje and initializing it in PolylinjeIterator 's constructor. 如果您需要在Polylinje类的实例中访问horn属性,则需要使该实例对PolylinjeIterator类可访问,可能是通过为它提供类Polylinje的属性并在PolylinjeIterator的构造函数中对其进行初始化来PolylinjeIterator

You also appear to use the horn identifier in three different ways: as a method of class PolylinjeIterator , as a local variable in this method and possibly as an attribute of class Polylinje ; 您似乎还可以通过三种不同的方式使用horn标识符:作为PolylinjeIterator类的方法,作为该方法中的局部变量,并且可能作为Polylinje类的属性; this is likely one source of confusion you should try to remove. 这可能是您应该消除的混乱之源之一。

In Java, you use the phrase Foo.this to refer to the enclosing type from within an anonymous class. 在Java中,使用短语Foo.this可以指代匿名类中的封闭类型。 See this question for more details. 有关更多详细信息,请参见此问题

You are not in this situation. 您不在这种情况下。

Based on your latest question edit, you need to just call the getters. 根据您最新的问题编辑,您只需要致电getter。 Eg: 例如:

if (Polylinje.this.horn.length > 0) // ERROR HERE!

should become: 应该变成:

if (polylinje.getHorn().length > 0)

Which will work if you have a field in your class called polylinje that you insert during your constructor, for instance: 如果您在类中有一个名为polylinje的字段,可以在构造函数中插入该字段,则该方法将起作用,例如:

public class PolylinjeIterator { 
    private int aktuell = -1; 
    private final Polylinje polylinje;

    public PolylinjeIterator (Polylinje polylinje){ 
        this.polylinje = polylinje;
        if (polylinje.getHorn().length > 0) 
            aktuell = 0; 
    } 

Polylinje.this means that you are accessing 'this' instance in Polylinje class and/or inside an inner class of Polylinje , this is useful when you use inside a non-static inner class (member class)/anonymous inner class of Polylinje . Polylinje.this意味着你在访问“这个”实例Polylinje类和/或内部类的内部Polylinje ,当使用一个非静态内部类(成员类)的内部/匿名内部类的,这是有用Polylinje A solution is to create an instance of Polylinje in PolylinjeIterator and access horn through an accessor or do the required operation in Polylinje , or maybe declare horn in PolylinjeIterator . 一种解决方案是在PolylinjeIterator创建Polylinje的实例,并通过访问horn访问horn或在Polylinje执行所需的操作,或者也许在PolylinjeIterator声明horn

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 范围内不能访问该类型的封闭实例 - No enclosing instance of the type is accessible in scope Java-范围内没有任何封闭类型的实例… - Java - No enclosing instance of the type … is accessible in scope 作用域bukkit编码中无法访问该类型的封闭实例 - No enclosing instance of the type is accessible in scope bukkit coding 不能访问类型为...的封闭实例 - No enclosing instance of type … is accessible 制作按钮时,作用域中无法访问MainActivity类型的封闭实例 - No enclosing instance of the type MainActivity is accessible in scope when making button 在MainActivity.this范围内无法访问MainActivity类型的封闭实例。 - No enclosing instance of the type MainActivity is accessible in scope at MainActivity.this 没有 TObjectHash 类型的封闭实例<T>可以在范围内访问。 龙头插件 - No enclosing instance of the type TObjectHash<T> is accessible in scope. Spigot plugin 作用域中无法访问类型为OuterClass.StaticNestedClass的封闭实例 - No enclosing instance of the type OuterClass.StaticNestedClass is accessible in scope 范围内没有任何封闭类型的实例… - no enclosing instance of type… in scope 没有可访问的MainRender类型的封闭实例 - No enclosing instance of type MainRender is accessible
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM