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

时间:2011-11-24 02:38:08

标签: c#

嗨,我是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
    }

这是randomEncounter,我提取对象

    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];
        }
    }

帮助真的很感激.. !! 提前致谢

4 个答案:

答案 0 :(得分:4)

理想情况下,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

这将允许您使用多态。

答案 1 :(得分:0)

你需要使用接口...... IMonster ..... IMonster然后有一个名字

然后让你的所有怪物实施IMonster

并且只有一个IMonsters列表

答案 2 :(得分:0)

您也可以尝试使用界面!看看......

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();
    }
}

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

答案 3 :(得分:0)

  

理想情况下,AngryFarmerUgly_SpiderGollum都应该继承   来自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
    }
}

我希望能帮助你。

相关问题