C# 需要解释一下这些代码吗

C# 需要解释一下这些代码吗,c#,sliding-tile-puzzle,C#,Sliding Tile Puzzle,我最近偶然发现了一个项目(使用*alg的8-puzzle solver),其中一些代码对我来说很奇怪,因为我以前从未见过类似的代码 这条线是什么意思?这是什么 this[StateIndex] 这个符号是什么?我根本无法理解它! 我贴了一个班级的样本,这样你们几乎可以一起看到。 还有一个问题,像StateNode这样实现一个类不是不对吗?它只使用了一个构造函数来初始化它的字段,但最糟糕的是,它将所有字段都声明为公共的!他/她是否应该为此任务实施Properties public enum Di

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

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

this[StateIndex]
这个符号是什么?我根本无法理解它! 我贴了一个班级的样本,这样你们几乎可以一起看到。 还有一个问题,像StateNode这样实现一个类不是不对吗?它只使用了一个构造函数来初始化它的字段,但最糟糕的是,它将所有字段都声明为公共的!他/她是否应该为此任务实施Properties

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;
}
}
公共枚举方向
{
上=1,下=2,左=3,右=4,上=5,下=6,左=7,右=8,停=9
}
类StateNode
{
公共int家长;
公开名单儿童;
公众引导行动;
公共指示和行动;
公共字节[,]状态;
公共字节深度;
公共字节空行;
公共字节空字符;
公共状态节点()
{ }
公共状态节点(int NewParent、Direction NewMove、Direction ParentMove、byte NewDepth、byte NewNullRow、byte NewNullCol)
{
this.Parent=NewParent;
State=新字节[5,5];
this.Move=NewMove;
this.ParentMove=ParentMove;
this.Depth=NewDepth;
this.NullRow=NewNullRow;
this.NullCol=NewNullCol;
this.Childs=新列表();
}
}
类StateTree:List
{
公共静态长MakedNodes;
公共静态长检查节点;
公共静态字节最大深度;
公共列表成功者1(int StateIndex)
{
List RetNodes=new List();
StateNode NewState=newstatenode();
//向上
if(此[StateIndex].NullRow+1 StateTree.MaxDepth)
StateTree.MaxDepth=NewState.Depth;
}
//向下
//左
//对
返回节点;
}
}
在您的具体案例中,它只是对元素的访问,因为它在类中使用,而类是从
列表中派生出来的

但它也可以是启用对类对象的索引访问的

例如,像这样声明类:

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

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

此[StateIndex]
指向类中的元素。因为
StateTree
继承自
列表
,所以您有一个可以通过索引访问的集合(在本例中是
this[N]
,其中
N
是元素的索引。

它的
索引属性
在C#net中


您可以查看教程:查看此处

此[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;
     }
  }
}

至于statenode,如果它是类(助手)的本地对象,那么你可以认为它是可以的,但我不喜欢,再花十分钟的时间,它就可以正常完成。如果它在程序集中是公共的,那么我认为它是不可接受的。但这是一种观点。

this[StateIndex]
正在使用当前类的indexer属性。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")
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
而言,它在技术上并没有什么问题,而且有一个初始化所有字段值的构造函数也很常见,但最好使用属性而不是公共字段。

如果您在理解特定语法方面有困难,就不必发布整个代码。祝您好运!也许我误解了,但是
StateIndex
中用作索引。它将检索数组中位于该位置的项。非常感谢(以及其他帮助过的人:))好的,让我们看看我是否正确。如果我声明了一个没有索引器的类,我就不能有这样的东西:MyClass[]MyClass=newmyclass[5];正当为了做到这一点,我需要使用这些方法,对吗!我感到困惑,在我看到的所有关于索引器的示例中,已经创建了一个对象!然后,该对象被索引!此类中尚未声明任何对象!:请参见此处的示例:--------------class ParentClass{private string[]range=new string[5];public string this[int indexrange]{get{return range[indexrange];}set{range[indexrange]=value;}@侯赛因:我更新了我的答案,希望能回答你的第二个问题。谢谢你,先生:)我真的很感激:)
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")
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"