2010-09-22 72 views
0

感謝您花時間閱讀。 =)非法轉發引用,對象可見性問題將編譯

我遇到的問題主要是對象可見性。我創建對象並嘗試在它們被初始化或在其中有任何數據之前創建它們的引用。您可以通過提供的代碼瞭解我想要做的事情。

某些方塊可以看到其他方塊等現在我的代碼編譯!問題是我有getter-methods從這些對象返回null。我嘗試將所有初始化放置在Square-class聲明中,但是這是我得到「非法向前參考」的地方。因此,最後,在代碼塊的結尾處,您會注意到我如何嘗試訪問這些對象及其引用。所有四個返回null,我要去的是一個指示存在的參考。

我確實明白這裏出了什麼問題,也許我的方法與對象不正確?我想,我的業餘技能主要是什麼阻止我在這一個=)

class Puzzle extends JFrame 
{ 

    Square square1; 
    Square square2; 
    Square square3; 
    Square square4; 
    Square square5; 
    Square square6; 
    Square square7; 
    Square square8; 
    Square square9; 
    Square square10; 
    Square square11; 
    Square square12; 
    Square square13; 
    Square square14; 
    Square square15; 
    Square square16; 

    public Puzzle() 
    { 

     PuzzleListener plist = new PuzzleListener(); 
     JPanel puzzle_board = new JPanel(new GridLayout(4,4,5,5)); 

     //randomize the text index's and the starting black square. 

     //   Square(north, south, east,west,isblack,text) 
     square1 = new Square(null,square2,square5,null,false,"1"); 
     square2 = new Square(null,square3,square6,square1,false,"2"); 
     square3 = new Square(null,square4,square7,square2,false,"3"); 
     square4 = new Square(null,null,square8,square3,false,"4"); 
     square5 = new Square(square1,square6,square9,null,false,"5"); 
     square6 = new Square(square2,square7,square10,square5,false,"6"); 
     square7 = new Square(square3,square8,square11,square6,false,"7"); 
     square8 = new Square(square4,null,square12,square7,false,"8"); 
     square9 = new Square(square5,square10,square13,null,false,"9"); 
     square10 = new Square(square6,square11,square14,square9,false,"10"); 
     square11 = new Square(square7,square12,square15,square10,false,"11"); 
     square12 = new Square(square8,null,square16,square11,false,"12"); 
     square13= new Square(square9,square14,null,null,false,"13"); 
     square14 = new Square(square10,square15,null,square10,false,"14"); 
     square15 = new Square(square11,square16,null,square14,false,"15"); 
     square16 = new Square(square12,null,null,square15,false,"16"); 

} 



class Square extends JPanel 
{ 

    //visible lable 
    JLabel lblText; 

    //internal object ID 
    int id; 

    //Define the visible neighbors 
    Square north; 
    Square south; 
    Square east; 
    Square west; 

    //is the square black 
    boolean isBlack; 

    public Square(String text) 
    { 

     lblText = new JLabel(text); 
     //setLayout(new GridLayout(5,5)); 
     lblText.setLocation(10,10000); 
     add(lblText); 
    } 

    public Square(Square n,Square e,Square s,Square w,boolean black, String text) 
    { 

     north = n; 
     east = e; 
     south = s; 
     west = w; 

     isBlack = black; 

     lblText = new JLabel(text); 
     //setLayout(new GridLayout(5,5)); 
     lblText.setLocation(10,10000); 
     add(lblText); 

    }//end square 

    Square getNorthNeighbor() 
    { 
     return north; 
    } 
    Square getSouthNeighbor() 
    { 
     return south; 
    } 
    Square getEastNeighbor() 
    { 
     return east; 
    } 
    Square getWestNeighbor() 
    { 
     return west; 
    } 
    boolean getIsBlack() 
    { 
     return isBlack; 
    } 


} 


System.out.println("Panel 1 has been clicked and has the neighbors " + "North: " + square1.getNorthNeighbor() + "East: " + square1.getEastNeighbor() + "South: " + square1.getSouthNeighbor() + "West: " + square1.getWestNeighbor()); 
+0

當你有相同 – irreputable 2010-09-22 22:44:37

+0

類型@irreputable的16個變量,這不是你真正的問題 - 在上下文中,這是一個完全有效的用例爲同一類型的16個變量。變量(與它們引用的對象不同)僅在初始化期間使用。它可能看起來不夠優雅,但在這種情況下,使用數組更具可讀性。 – 2010-09-22 23:42:52

回答

1

鑑於他們需要了解對方,爲什麼不先創建所有的方塊,然後有一個setNeighbours(Square north, Square south, ...)方法,你可以當你真的創建了所有的對象時調用?從不變性的角度來看,這並不好(一般來說這是一種類型的不錯的屬性),但從根本上說,你有循環引用。

換句話說,假設您必須創建兩個Person實例,並向每個實例表明它們是兄弟。你可以這樣做:

Person billy; 
Person bobby; 
billy = new Person(bobby); 
bobby = new Person(billy); 

但你可以做:

Person billy = new Person(); 
Person bobby = new Person(); 
billy.setBrother(bobby); 
bobby.setBrother(billy); 

它,如果你能避免要求在第一時間循環引用更好,但有時這只是事情的方式。

+0

嘿,給我留下簡單的! :) – willcodejavaforfood 2010-09-22 22:10:08

+0

感謝喬恩,因爲沒有想到這一點,我真是太蠢了。我聽到關於循環引用的ya,我的大腦更容易繞過目前我遇到的問題。 – 2010-09-22 22:11:55

1

只是使用setter,而不是試圖在構造函數中這樣做,因爲那樣做根本不可能。

square1 = new Square(); 
square2 = new Square(); 
square3 = new Square(); 

square1.setNorth(square2); 
square1.setSouth(square3); 
... 
+0

謝謝,但喬恩擊敗雅=) – 2010-09-22 22:12:35