我最近偶然发现了一个项目(使用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 :(得分: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 :(得分:2)
在C#.net中的Indexed Properties
你可以查看教程:http://msdn.microsoft.com/en-us/library/aa288464(v=vs.71).aspx点击这里
答案 2 :(得分:2)
this[StateIndex]
正在使用当前班级&#39;索引器属性。 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>
,它实现了索引器属性。
但是不要在具有索引器属性和数组的类之间感到困惑。尽管语法类似,但数组是完全不同的东西。数组是可以通过索引访问的对象列表。索引器属性是充当数组的单个对象的未命名属性。因此,例如,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
而言,它在技术上没有任何问题,并且拥有一个初始化所有字段值的构造函数并不罕见,但它总是如此最好使用属性而不是公共字段。
答案 3 :(得分:1)
this[StateIndex]
指向类中的元素。由于StateTree
继承自List<T>
,因此您有一个可通过索引访问的集合(在本例中为this[N]
,其中N
是元素的索引。
答案 4 :(得分: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,如果它是该类的本地(帮助者),那么你可以认为它没关系,我不喜欢它,另外十分钟的工作可以正常完成。如果它在集会中是公开的,那么在我看来它是不可接受的。但这是一种意见。