0

我最近偶然发现了一个项目(使用 A* alg 的 8 谜题求解器),其中一些代码对我来说很奇怪,因为我以前从未见过类似的代码。

这条线是什么意思?这是什么 ?!

this[StateIndex]

这是什么符号?我根本无法理解!我发布了该课程的示例,以便您几乎可以一起看到它。还有一个问题,像 StateNode 这样实现一个类是不是错了?它仅使用构造函数来初始化其字段,更糟糕的是,将它们全部声明为 public !他/她应该没有为此任务实施Propertise吗?

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;
}
}
4

5 回答 5

3

在您的具体情况下,它只是访问元素,因为它在派生List<T>

但它也可以是索引器,它可以对您的类对象进行索引访问。

例如像这样声明类:

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
于 2012-07-06T13:03:34.153 回答
2

Indexed Properties在 C# .net 中。

您可以查看教程: http: //msdn.microsoft.com/en-us/library/aa288464 (v=vs.71).aspx在这里查看

于 2012-07-06T13:05:37.740 回答
2

this[StateIndex]正在使用当前类的索引器属性。indexer 属性允许您像访问数组一样访问集合或列表对象中的元素。例如:

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>which 确实实现了索引器属性。

但是不要混淆具有索引器属性的类和数组。数组是完全不同的东西,尽管语法相似。数组是可以通过索引访问的对象列表。索引器属性是充当数组的单个对象的未命名属性。例如,List<string>有一个 indexer 属性,因此您可以使用与数组索引相同的语法访问它包含的项目(如上例所示)。但是,您仍然可以创建对象数组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而言,它在技术上没有任何问题,并且拥有一个初始化所有字段值的构造函数并不罕见,但使用属性而不是公共字段总是更好。

于 2012-07-06T13:18:33.987 回答
1

this[StateIndex]指向类中的一个元素。因为StateTree继承自 a List<T>,所以您有一个可以通过索引访问的集合(在这种情况下this[N]N元素的 index.

于 2012-07-06T13:04:19.457 回答
1

this[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,如果它是班级的本地节点(助手),那么您可以认为它还可以,但我不喜欢它,再过十分钟的工作就可以正确完成。如果它在议会中公开,那么我认为这是不可接受的。但这是一种意见。

于 2012-07-06T13:16:17.310 回答