出于培训目的,我正在遵循为 Java 编写的教程,到目前为止我已成功将其“翻译”为 C#,但是,我现在面临一个问题,我真的不知道如何解决它。我能找到的最接近(?)可能的答案是这个问题。尽管我现在在理解代表和兰巴表达式方面存在问题。无论如何,这里是Java中的相关代码:
public abstract class LevelUpOption {
private String name;
public String name() { return name; }
public LevelUpOption(String name){
this.name = name;
}
public abstract void invoke(Creature creature);
}
Run Code Online (Sandbox Code Playgroud)
另一个类:
public class LevelUpController {
private static LevelUpOption[] options = new LevelUpOption[]{
new LevelUpOption("Increased hit points"){
public void invoke(Creature creature) { creature.gainMaxHp(); }
},
new LevelUpOption("Increased attack value"){
public void invoke(Creature creature) { creature.gainAttackValue(); }
},
new LevelUpOption("Increased defense value"){
public void invoke(Creature creature) { creature.gainDefenseValue(); …Run Code Online (Sandbox Code Playgroud) 我有以下代码:
class Tile
{
public TCODColor color { get; protected set; }
public int glyph { get; protected set; }
public Boolean isDigable()
{
return this.Equals(typeof(Wall));
}
public Boolean isGround()
{
return this.Equals(typeof(Floor));
}
}
Run Code Online (Sandbox Code Playgroud)
Wall和Floor类都继承自Tile.在程序的另一个点上,我有一个if语句,例如:
public void dig(int x, int y)
{
if (tiles[x, y].isDigable())
{
tiles[x,y] = new Floor();
}
}
Run Code Online (Sandbox Code Playgroud)
tile是Tile类的二维数组,它们的内容被初始化为Floor或Wall.因此,如果tile是Wall,它是Digable(并且应该返回true)但是无论它总是返回false,因此,不执行其他代码.由于我不熟悉C#,我想我做错了.语法明智,有什么建议吗?
我有一个列表,并希望顺利迭代它,同时删除一个接一个元素.我以为我可以这样做:
List<Point> open = new List<Point>();
...
while (!(open == null))
{
Point p = open.RemoveAt(0);
...
Run Code Online (Sandbox Code Playgroud)
但是,我不希望它如何工作,从"不能隐式地将类型'void'转换为'Point'开头".但是,在删除它/使其无效之前,RemoveAt的调用是否应该指向P?