gpt4 book ai didi

c# - 结构常量还是类?

转载 作者:行者123 更新时间:2023-12-04 05:14:09 24 4
gpt4 key购买 nike

我需要存储一个有 5 个方向的数组,我想知道什么是最易读的方式。
我可以做阵列

 int myDirection[100] = {0,1,2,3,4,5,4,4,3,4,0......etc  // poor readability 

或者我可以使用常量并做
const up = 1;
const down = 2; //... etc
int myDirection[100] = {up, down, center, right, left, .....etc

或者我可以写一个结构
 struct direction
{ private byte mydirectionval;
{
public void direction Setup(){mydirection =1;}
public void direction Setright(){mydirection=2;}
public void direction Setdown(){mydirection=3;}
public void direction Setleft(){mydirection=4;}
public void direction Setcenter(){mydirection=0;}
public direction Getdirection(){....

}
direction newpath[100] = { ......

或者我应该写一个类
     public static class direction   // not sure use static here ?
{ private int direction =0;
public const int Up = 1;
public const int left =2;
public const int down = 3;
public const int right = 4;
public const int center = 0;
public int getdirection(){return direction}
public void int setdirection(int x){direction=x}
}
direction mydir[100] = new direction;
mydir[0].setdirection(up);
// or use it like
mydir[0]=mydir.up //==> i prefer readability of code so this looks better?

好的,人们建议使用枚举,但是我需要一个枚举数组 mydir[100]
怎么做??

最佳答案

使用 enum .

enum Direction 
{
up = 1,
down = 2,
center = 3,
right = 4,
left = 5
}

您可以在数组中使用它:
Direction[] myDirection = { Direction.left, Direction.up, Direction.right ... };


myDirection[0] = Direction.left; ...

关于c# - 结构常量还是类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14498835/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com