通过基础检查实例 class
Checking for instances through the base class
现在我正在做一款需要怪物的游戏。我做了一个 MonsterBase
class 其他怪物都会继承的。这是一个主机游戏,所以我给每个 LowLevelMonster
个实例不同的坐标。
在另一种方法中,我想检查玩家是否与其中一个怪物实例在同一坐标上。现在我只能使用 if
语句来请求每个怪物实例的 PositionX
& PositionY
。如果我有超过 5 个怪物,这个规模会非常大。
我的问题:由于其他怪物实例继承自MonsterBase
,是否可以检查PositionX
& PositionY
MonsterBase
需要 每个 它的继承实例?
类似于:
if(Player.PositionX == MonsterBase.PositionX)
{
Console.WriteLine("the names of the Monsters instances that have the same X Coordinates");
}
这些是我目前的 classes:
public class MonsterBase
{
int PositionX = 0;
int PositionY = 0;
}
public class LowLevelMonster : MonsterBase
{
string name = "monster";
int HP = 5;
}
不,这不是继承的工作方式,没有隐含的 link 从基 class 的实例到子实例的实例。
你将不得不在一个集合中记录你所有的怪物,比如 List<MonsterBase>
或 Collection<MonsterBase>
然后你可以过滤列表。
listOfMonsters.Where(monster => monster.PositionX == Player.PositionX)
什么是lambda表达式
将 lambda 表达式视为 shorthand 编写方法的方式。
将表达式 monster => monster.PositionX == Player.PositionX
移动到一个变量会得到你
Expression<Func<MonsterBase, bool>> lambda =
monster => monster.PositionX == Player.PositionX;
listOfMonsters.Where(lambda);
这样更容易看出发生了什么。 listOfMonsters.Where()
需要一个 Expression<Func<MonsterBase, bool>>
类型的参数,其中 lambda 本质上是一个方法委托,它接受一个参数(名为 monster
)和 returns 一个布尔值。
这也可以写成
public class Player() {
public int PositionX { get; set; }
public bool ComparePosition(MonsterBase monster){
return monster.PositionX == Player.PositionX;
}
}
// then later in the code
listOfMonsters.Where(Player.ComparePosition);
这是 Where
的不同重载,它采用的方法组与
基本相同
listOfMonsters.Where(monster => Player.ComparePosition(monster));
现在我正在做一款需要怪物的游戏。我做了一个 MonsterBase
class 其他怪物都会继承的。这是一个主机游戏,所以我给每个 LowLevelMonster
个实例不同的坐标。
在另一种方法中,我想检查玩家是否与其中一个怪物实例在同一坐标上。现在我只能使用 if
语句来请求每个怪物实例的 PositionX
& PositionY
。如果我有超过 5 个怪物,这个规模会非常大。
我的问题:由于其他怪物实例继承自MonsterBase
,是否可以检查PositionX
& PositionY
MonsterBase
需要 每个 它的继承实例?
类似于:
if(Player.PositionX == MonsterBase.PositionX)
{
Console.WriteLine("the names of the Monsters instances that have the same X Coordinates");
}
这些是我目前的 classes:
public class MonsterBase
{
int PositionX = 0;
int PositionY = 0;
}
public class LowLevelMonster : MonsterBase
{
string name = "monster";
int HP = 5;
}
不,这不是继承的工作方式,没有隐含的 link 从基 class 的实例到子实例的实例。
你将不得不在一个集合中记录你所有的怪物,比如 List<MonsterBase>
或 Collection<MonsterBase>
然后你可以过滤列表。
listOfMonsters.Where(monster => monster.PositionX == Player.PositionX)
什么是lambda表达式
将 lambda 表达式视为 shorthand 编写方法的方式。
将表达式 monster => monster.PositionX == Player.PositionX
移动到一个变量会得到你
Expression<Func<MonsterBase, bool>> lambda =
monster => monster.PositionX == Player.PositionX;
listOfMonsters.Where(lambda);
这样更容易看出发生了什么。 listOfMonsters.Where()
需要一个 Expression<Func<MonsterBase, bool>>
类型的参数,其中 lambda 本质上是一个方法委托,它接受一个参数(名为 monster
)和 returns 一个布尔值。
这也可以写成
public class Player() {
public int PositionX { get; set; }
public bool ComparePosition(MonsterBase monster){
return monster.PositionX == Player.PositionX;
}
}
// then later in the code
listOfMonsters.Where(Player.ComparePosition);
这是 Where
的不同重载,它采用的方法组与
listOfMonsters.Where(monster => Player.ComparePosition(monster));