2012-07-06 80 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如果是本地的类(帮手),那么你可以认为它好,我不喜欢它虽然,十分钟的工作就可以做好。如果它在大会上公开,那么在我看来这是不合适的。但这是一个意见。