[英]no enclosing instance of the type Polylinje is accessible in scope
首先,對我的代碼中的瑞典語表示抱歉。 這是學校的作業,他們用瑞典語編寫...我希望代碼易於理解。
我在代碼的三行中收到此錯誤,卻不知道為什么。
no enclosing instance of the type Polylinje is accessible in scope
我的代碼是:
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;
}
}
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) {}
}
如果要在PolylinjeIterator
中Polylinje
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;
}
如果要在PolylinjeIterator
類的不同位置使用Polylinje
,請創建一個類成員,並將給定實例分配給構造函數中的該成員。 然后在PolylinjeIterator
類中使用該成員。
使用Polylinje.this
是沒有意義的,因為Classes
沒有自己的成員作為實例。 實例為你所創造的作為你的類的具體實體,所以當你表示this
是不需要的類名
您代碼中的Polylinje.this.horn
無效。 如果您需要在Polylinje
類的實例中訪問horn
屬性,則需要使該實例對PolylinjeIterator
類可訪問,可能是通過為它提供類Polylinje
的屬性並在PolylinjeIterator
的構造函數中對其進行初始化來PolylinjeIterator
。
您似乎還可以通過三種不同的方式使用horn
標識符:作為PolylinjeIterator
類的方法,作為該方法中的局部變量,並且可能作為Polylinje
類的屬性; 這可能是您應該消除的混亂之源之一。
在Java中,使用短語Foo.this
可以指代匿名類中的封閉類型。 有關更多詳細信息,請參見此問題 。
您不在這種情況下。
根據您最新的問題編輯,您只需要致電getter。 例如:
if (Polylinje.this.horn.length > 0) // ERROR HERE!
應該變成:
if (polylinje.getHorn().length > 0)
如果您在類中有一個名為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
意味着你在訪問“這個”實例Polylinje
類和/或內部類的內部Polylinje
,當使用一個非靜態內部類(成員類)的內部/匿名內部類的,這是有用Polylinje
。 一種解決方案是在PolylinjeIterator
創建Polylinje
的實例,並通過訪問horn
訪問horn
或在Polylinje
執行所需的操作,或者也許在PolylinjeIterator
聲明horn
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.