2012-07-06 56 views
0

我最近偶然發現了一個項目(使用A * alg的8-puzzle求解器),其中一些代碼對我來說很奇怪,因爲我從來沒有見過它的喜歡。需要對這些代碼位的解釋

這條線是什麼意思?這是什麼 ?!

this[StateIndex] 

這是什麼表示法?我根本無法理解它! 我發佈了一個樣本,以便您幾乎可以一起看到它。 還有一個問題,像StateNode一樣實現一個類是否沒有錯?它只使用一個構造函數來初始化它的字段,但是最糟糕的是,它們都是公開的!如果他/她沒有執行這項任務的功能?

public enum Direction 
{ 
    Up = 1, Down = 2, Left = 3, Right = 4, UpUp = 5, DownDown = 6, LeftLeft = 7, RightRight = 8, Stop = 9 
} 

class StateNode 
{ 
    public int Parent; 
    public List<int> Childs; 
    public Direction Move; 
    public Direction ParentMove; 
    public byte[,] State; 
    public byte Depth; 
    public byte NullRow; 
    public byte NullCol; 
public StateNode() 
{ } 
public StateNode(int NewParent, Direction NewMove, Direction ParentMove, byte NewDepth, byte NewNullRow, byte NewNullCol) 
{ 
    this.Parent = NewParent; 
    this.State = new byte[5, 5]; 
    this.Move = NewMove; 
    this.ParentMove = ParentMove; 
    this.Depth = NewDepth; 
    this.NullRow = NewNullRow; 
    this.NullCol = NewNullCol; 
    this.Childs = new List<int>(); 
} 
} 

class StateTree : List<StateNode> 
{ 
    public static long MakedNodes; 
    public static long CheckedNodes; 
    public static byte MaxDepth; 

    public List<int> Successor1(int StateIndex) 
    { 
     List<int> RetNodes = new List<int>(); 
     StateNode NewState = new StateNode(); 

     //Up 
    if (this[StateIndex].NullRow + 1 <= 3 && this[StateIndex].ParentMove != Direction.Up) 
    { 
     NewState = ChangeItemState(this[StateIndex], StateIndex, Direction.Up, Direction.Down, Convert.ToByte(this[StateIndex].Depth + 1), this[StateIndex].NullRow, this[StateIndex].NullCol, Convert.ToByte(this[StateIndex].NullRow + 1), this[StateIndex].NullCol); 

     this.Add(NewState); 
     RetNodes.Add(this.Count - 1); 
     StateTree.MakedNodes++; 
     this[StateIndex].Childs.Add(this.Count - 1); 
     if (NewState.Depth > StateTree.MaxDepth) 
      StateTree.MaxDepth = NewState.Depth; 

    } 
    //Down 
    //Left 
    //Right 

    return RetNodes; 
} 
} 
+0

如果您在理解特定語法時遇到困難,您並不需要發佈整個代碼。祝你好運! – SimpleVar 2012-07-06 13:05:17

+0

也許我誤解了,但'StateIndex'被用作'this'中的一個索引。它將檢索位於陣列中該位置的項目。 – Silvermind 2012-07-06 13:07:02

回答

2

this[StateIndex]正在使用當前類的索引器屬性。索引器屬性允許您像訪問數組一樣訪問集合或列表對象中的元素。例如:

List<string> strings = new List<string>(); 
strings.Add("Item 1"); 
strings.Add("Item 2"); 
strings.Add("Item 3"); 
string x = strings[0]; // Returns the first item in the list ("Item 1") 

當你想訪問你自己的類的索引器屬性,但是,你必須與this關鍵字前言它。您會注意到,在您的示例中,StateTree類未實現索引器屬性,因此可能會增加您的混淆。它的工作原因是因爲StateTree繼承自List<StateNode>,它實現了索引器屬性。

但是不要在具有索引器屬性和數組的類之間混淆。數組是完全不同的東西,儘管語法是相似的。數組是可以被索引訪問的對象列表。索引器屬性是充當數組的單個對象的未命名屬性。因此,例如,List<string>具有索引器屬性,因此您可以使用與數組索引相同的語法訪問它包含的項目(如上例所示)。但是,您仍然可以製作一組List<string>對象。因此,例如:

List<string> strings1 = new List<string>(); 
strings1.Add("Item 1.1"); 
strings1.Add("Item 1.2"); 

List<string> strings2 = new List<string>(); 
strings2.Add("Item 2.1"); 
strings2.Add("Item 2.2"); 

List<string>[] stringsArray = new List<string>[] { strings1, strings2 }; 
object result; 
result = stringsArray[0]; // Returns strings1 
result = stringsArray[0][1]; // Returns "Item 1.2" 
result = stringsArray[1][0]; // Returns "Item 2.1" 

至於StateNode去,沒有什麼技術上的錯誤與它的,這是不尋常的有初始化所有的字段值構造函數,但它總是更好地使用屬性,而不是公共領域。

+0

非常感謝(也幫助其他人:)) 好吧,讓我看看我是否有這個權利。如果我聲明一個沒有索引器的類,我不能擁有像: MyClass [] myclass = new MyClass [5]; 對不對?爲了能夠做到這一點,我需要使用這樣的方法嗎? ! – Breeze 2012-07-06 14:02:08

+0

我感到困惑,在所有關於索引器的示例中,已經創建了一個對象!然後,該對象被索引!沒有任何對象在這個類中聲明! : 請參閱此處的示例:http://www.c-sharpcorner.com/UploadFile/senthurganesh/109172007050741AM/1.aspx --------------------- class ParentClass { private string [] range = new string [5]; 公共字符串這個[INT indexrange] { {返回範圍[indexrange];} 集合{範圍[indexrange] =值; } } – Breeze 2012-07-06 14:11:09

+0

@Hossein我更新了我的答案,希望能回答你的第二個問題。 – 2012-07-06 14:14:27

3

在你具體情況它的元素只是訪問,因爲它是衍生List<T>

但它也可以indexer這使得指數的存取權限類內部使用您的類對象。

例如聲明類這樣的:

public class ListWrapper 
{ 
    private List<int> list = ... 

    public int this[int index] 
    { 
     return list[index]; 
    } 
} 

和使用後它像

var lw = new ListWrapper(); 
//fill it with data 

int a = lw[2]; //ACCESS WITH INDEX EVEN IF THE TYPE IS NOT COLLECTION BY ITSELF 
1

this[StateIndex]所指向的類中的元素。因爲從List<T>StateTree繼承,你有一個集合,是通過索引訪問(在這種情況下this[N]其中N是元素的索引

1

這個[StateIndex]是你如何給一個類和索引屬性如

public class IndexedClass 
{ 
    private List<String> _content; 
    public IndexedClass() 
    { 
    _content = new List<String>(); 
    } 
    public Add(String argValue) 
    { 
    _content.Add(argValue); 
    } 

    public string this[int index] 
    { 
    get 
    { 
     return _content[index]; 
    } 
    set 
    { 
     _content[Index] = value; 
    } 
    } 
} 

所以現在你可以做

IndexedClass myIndex = new IndexedClass(); 
myIndex.Add("Fred"); 
Console.Writeline(myIndex[0]); 
myIndex[0] = "Bill"; 
Console.Writeline(myIndex[0]); 

至於statenode如果是本地的類(幫手),那麼你可以認爲它好,我不喜歡它雖然,十分鐘的工作就可以做好。如果它在大會上公開,那麼在我看來這是不合適的。但這是一個意見。