从混合类型集合中获取对象

本文关键字:获取 取对象 集合 混合 类型 | 更新日期: 2023-09-27 18:15:48

你好,我是OOP新手,我需要帮助解决一个小问题。我使用了一个名为Monsters的集合来存储3种类型的对象。蜘蛛,农民,咕噜(无关紧要)。我的集合作为索引器,但当我使用它从集合中获取对象时,对象是无类型的,但我确实需要TypeCast我的下一个操作。

    private void Form1_Load(object sender, EventArgs e)
    {
        CurrentOpponent Opponent = new CurrentOpponent();
        Gollum myGollum = new Gollum();
        AngryFarmer myFarmer = new AngryFarmer();
        Ugly_Spider mySpider = new Ugly_Spider();
        myMonsters.AddGollum(myGollum);
        myMonsters.AddFarmer(myFarmer);
        myMonsters.AddUgly(mySpider);
        progressBar1.Increment(100);
        progressBar2.Increment(100);
        Monster myCurrentOpponent = Opponent.randomEncounter();
        //textBox1.Text = (this is where i need the type for a cast)myCurrentOpponent.name
    }

这是随机计数器,我提取对象

    class CurrentOpponent
    {
        public Monster randomEncounter()
        {
            Random _random = new Random();
            int opp = _random.Next(4);
            return myMonsters[opp];
        }

最后是索引器,它返回一个怪物(所有3种怪物类型的父类)

    public Monster this[int xxx]
    {
        get
        {
            return (Monster)List[xxx];
        }
    }

帮助将是非常感激的…!!提前感谢

从混合类型集合中获取对象

理想情况下,AngryFarmerUgly_SpiderGollum都应该继承Monster:

public class AngryFarmer : Monster
{
    // ...
}
// etc.

你可以直接使用List<Monster>:

myMonsters = new List<Monster>();
myMonsters.Add(new AngryFarmer()); // works because AngryFarmer is a kind of Monster

您需要使用接口......IMonster……IMonster有一个名字

然后让你所有的怪物执行IMonster

和List of IMonsters

你也可以尝试使用接口!看一看……

public interface IMonster
{
    String Name { get; }
    Int32 Health { get; set; }
}
public class Spider : IMonster
{
    public Spider()
    {
        _health = 100;
    }
    public string Name
    {
        get { return "Spider"; }
    }
    private int _health;
    public int Health
    {
        get { return _health; }
        set { _health = value; }
    }
}
public class Gollum : IMonster
{
    public Gollum()
    {
        _health = 250;
    }
    public string Name
    {
        get { return "Gollum"; }
    }
    private int _health;
    public int Health
    {
        get { return _health; }
        set { _health = value; }
    }
}
class Program
{
    static void Main(string[] args)
    {
        List<IMonster> monsters = new List<IMonster>()
                                      {
                                          new Gollum(),
                                          new Spider()
                                      };
        IMonster randomMonster = GetRandomMonster(monsters);
        Console.WriteLine(randomMonster.Name + "/" + randomMonster.Health);
    }
    private static IMonster GetRandomMonster(List<IMonster> monsters)
    {
        //Your code for getting a random monster goes here!
        throw new NotImplementedException();
    }
}

我非常喜欢这种方法…想象一下,你的游戏中有一个最初并不是怪物的元素。假设这是你游戏中的一个随机元素,在特定事件之后它会变成一个怪物,你的英雄(游戏邦注:比如强大和魔法的英雄)必须与之战斗。如果你在创造游戏后很长一段时间才决定添加这个功能,那么改变它就会变得有害/困难/有风险,因为这个元素可能已经从另一个类中继承了。如果你使用的是接口,你只需要在这个实体上执行它,它就能像游戏中的其他怪物一样迅速行动。这意味着这个随机实体可以作为参数传递给方法Fight(IHero hero, IMonster monster);

理想情况下,AngryFarmer, Ugly_SpiderGollum都应该继承from Monster

我知道你的问题就像俄罗斯方块游戏中的问题:1/你有怪物,我有形状。2/每种怪物都有自己的属性(生命值、魔法点等)和行为(攻击、奔跑、施放法术等),就像方块有属性(颜色、位置、状态等)一样行为(向下,向右旋转,向左旋转,…)

在游戏场景中,你想随机一个具有特定属性和行为的怪物,就像我想随机一个形状。如果这是你的问题,你可以试试我的代码:

public abstract class CMonster
{
    int _HP;
    int _MP;
    //..and something like this
    public int HP
    {
        get { return this._HP; }
        set { this._HP=value;}
    }
    public int MP
    {
        get { return this._MP; }
        set { this._MP = value; }
    }
    public abstract void Run();
    public abstract void Attach();
    public abstract void CastSpell();
}
public class CUgly_Spider : CMonster
{
    public CUgly_Spider()
    {
        this.MP = 100;//your value here
        this.HP = 100;//your value here
    }
    public override void Attach()
    {
        //your implemetation here
    }
    public override void Run()
    {
        //your implemetation here
    }
    public override void CastSpell()
    {
        //your implemetation here
    }
}

public class CGollum : CMonster
{
    public CGollum()
    {
        this.MP = 100;//your value here
        this.HP = 100;//your value here
    }
    public override void Attach()
    {
        //your implemetation here
    }
    public override void Run()
    {
        //your implemetation here
    }
    public override void CastSpell()
    {
        //your implemetation here
    }
}
class Test
{
    private void InitTheGame()
    {
        CMonster curMonster=null;
        Random rnd = new Random();
        //sample random
        if ((rnd.Next() % 2) == 0)
        {
            curMonster = new CGollum();
        }
        else
        {
            curMonster = new CUgly_Spider();
        }
        curMonster.Run();//when (rnd.Next() % 2) == 0 then the Gollum is doing else the Ugly_Spider
        curMonster.Attach();//when (rnd.Next() % 2) == 0 then the Gollum is doing else the Ugly_Spider
        curMonster.CastSpell();//when (rnd.Next() % 2) == 0 then the Gollum is doing else the Ugly_Spider
    }
}

希望对你有帮助。