无法访问具有类属性的c#foreach循环
我有一个foreach循环,它遍历一个类的数组,并有一个无法访问具有类属性的c#foreach循环,c#,foreach,C#,Foreach,我有一个foreach循环,它遍历一个类的数组,并有一个if语句,该语句使用该类的一个属性,但是它不起作用。我得到这个错误: 无法使用实例引用访问Board.Boat.length;改为使用类型名称限定它 你怎么解决这个问题?这是代码。记住,它还没有完成 using System; using System.Collections.Generic; using System.Numerics; namespace BattleShip { class Board {
if
语句,该语句使用该类的一个属性,但是它不起作用。我得到这个错误:
无法使用实例引用访问Board.Boat.length;改为使用类型名称限定它
你怎么解决这个问题?这是代码。记住,它还没有完成
using System;
using System.Collections.Generic;
using System.Numerics;
namespace BattleShip
{
class Board
{
class Boat
{
// This class is made for storing the meta-data of each individual boat
// Determines the start point
static int[,] pos = new int[xBoardSize, yBoardSize];
// Determines the direction of the boat (0 = horizontal, 1 = vertical)
static int direction = 0;
// Keeps track of the boat's size
static int length = 1;
// Keeps track if tile is hit or untouched
static int[] stateList;
// Searches fora oat object and
static Boat SearchForBoat (Vector2 coords, Boat[] database)
{
foreach (Boat boat in database)
{
for (int i = 0; i < boat.length; i++)
}
}
}
// These variables determine the size of the board
static int xBoardSize = 10;
static int yBoardSize = 10;
}
}
使用系统;
使用System.Collections.Generic;
使用系统数字;
名称空间战舰
{
班级委员会
{
班船
{
//该类用于存储每艘船只的元数据
//确定起点
静态int[,]pos=新int[xBoardSize,yBoardSize];
//确定船的方向(0=水平,1=垂直)
静态int方向=0;
//跟踪船的大小
静态整数长度=1;
//跟踪瓷砖是否被击中或未被触碰
静态int[]状态列表;
//搜索oat对象和
静态船搜索船(矢量2坐标,船[]数据库)
{
foreach(数据库中的船)
{
对于(int i=0;i
这是因为length
属性是static
。此类属性相当于类属性,而其他属性则是实例属性,即静态
字段与类型一起使用(另一个与类的实例化对象一起使用)。在您的情况下,它应该是Boat.length
,而不是Boat.length
注释:您应该考虑这些静态属性,如<代码>长度>代码>,我觉得它应该依赖于特定的对象,因此不应该是代码>静态< /代码>。
错误告诉您,您试图从对象实例访问静态属性。foreach (Boat boat in database) // give me each real boat from database
{
for (int i = 0; i < boat.length; i++) // access an individual boat length
我对您的代码有多个注释: 第一个静态被认为是独一无二的。它类似于所有对象实例共享的全局值 从 即使未创建类的实例,静态成员也可以在类上调用。静态成员总是通过类名而不是实例名进行访问无论创建了多少个类实例,静态成员只存在一个副本 考虑到这一点,您的
船将成为:
public class Boat
{
// This class is made for storing the meta-data of each individual boat
// Determines the start point
public int[,] pos = new int[xBoardSize, yBoardSize];
// Determines the direction of the boat (0 = horizontal, 1 = vertical)
public int direction = 0;
// Keeps track of the boat's size
public int length = 1;
// Keeps track if tile is hit or untouched
public int[] stateList;
}
您的船现在应该成为您的董事会的一部分,具有Has-a
关系:
public class Board
{
private Boat[] boats;
public Board(Boat[] boats) {
// initialize th boats for the specific game
this.boats = boats
}
Boat SearchForBoat (Vector2 coords)
{
foreach (Boat boat in boats)
{
for (int i = 0; i < boat.length; i++)
}
}
// These variables determine the size of the board
int xBoardSize = 10;
int yBoardSize = 10;
}
公共课程委员会
{
私人船只[]艘;
公众船(船[]艘){
//为特定的游戏初始化th船
这条船
}
船搜索船(矢量2坐标)
{
foreach(船中船)
{
对于(int i=0;i
然后需要通过构造函数初始化并使用特定实例
即使没有创建类的实例,静态成员也可以在类上调用。静态成员总是通过类名而不是实例名进行访问
for(int i=0;i
阅读以了解更多信息如果将所有静态
成员和方法转换为实例,您将解决问题,仅在绝对需要时使用静态
<代码>船
应该是独立的。。。想想看。如果你造两条船,它们的长度应该相同吗?如果你移动一个船的位置,那应该移动所有其他船的位置吗?
public class Board
{
private Boat[] boats;
public Board(Boat[] boats) {
// initialize th boats for the specific game
this.boats = boats
}
Boat SearchForBoat (Vector2 coords)
{
foreach (Boat boat in boats)
{
for (int i = 0; i < boat.length; i++)
}
}
// These variables determine the size of the board
int xBoardSize = 10;
int yBoardSize = 10;
}
for (int i = 0; i < boat.length; i++) //wrong access static from an instance
for (int i = 0; i < Boat.length; i++) //correct access static from the type itself (class itlsef)