正确访问“:Base”构造函数中的参数

时间:2015-07-12 01:08:22

标签: c# class oop constructor

场合

我正在使用我在本学年学到的一些OOP练习制作一个基于文本的小游戏,我之所以这样做,主要是为了提高我目前所知的OOP,并希望在经验丰富的帮助下改进我的一些练习用户。

所以,通俗地说,我目前有一个“Ork”课程正在扩展“Enemy”和“Enemy”正在扩展“Entity”

public class Ork : Enemy

public class Enemy : Entity

我有“Enemy”类,因为我打算制作许多独特的敌人对象类型,而不仅仅是“Ork”,通过这种当前的设置,我可以用同样的方式制作“Elf”或“Human”。 (并且将来添加新类型要容易得多)敌人拥有它自己的构造函数。

public Enemy(string[] Droplist, int Defaultgold, string Weaknesses, string Resistances, string[] Taunts, string Aggro, string Critchance, string Threshold, string Name, string Type,
        string Spawnzone, string _Class, int Defaultlevel, int Maxlevel, string trait, int str, int agi, int dex, int hel, int man)
    {
        //make a new entity instance.
        entity = new Entity();

        //Set all the local variables to the passed in parameters.

        setEntity(Name, Type, Spawnzone, _Class, Defaultlevel, Maxlevel, trait, str, agi, dex, hel, man);

        droplist = Droplist;
        defaultgold = Defaultgold;
        weaknesses = Weaknesses;
        resistances = Resistances;
        taunts = Taunts;
        aggro = Aggro;
        critchance = Critchance;
        threshold = Threshold;

}

正如你所看到的,我将传入参数设置为局部变量,我使用setEntity为我做了一些工作。我也有一个“实体”类,其原因是,并非所有“Stats”都是敌人,因此我将“实体”作为中间人。

问题

问题出在这一类课程的最后,即Ork课程。

在其他程序中,例如我的Hillracing程序,我类似类的构造函数看起来像这样:

public JuniorMember(string stringfirstname, string stringlastname, string stringmiddlename, string stringtitle, string strst, string strtwn, string strpc, string strEmail, int intMobile, int intHome,
            string shrnumber, string memtype, string username, string password, int noracesrun, float perraceswon, string mempic, string memclub, string gender, int memexp, int yearofbirth, int monthofbirth, int dayofbirth, string nextofkin, string docName, string docTel, string healthIssues, string parentalConsent)
            : base(stringfirstname, stringlastname, stringmiddlename, stringtitle, strst, strtwn, strpc, strEmail, intMobile, intHome, shrnumber, memtype, username, password, noracesrun, perraceswon, mempic, memclub,
            gender, memexp, yearofbirth, monthofbirth, dayofbirth)
        {

所以,问题是,简单地说,在这个程序中,我正在查看类似数量的参数,对我个人来说,看看这些参数,我感觉不像我正在这样做。

我从Hillracing给出的例子完美无缺,但我只是想知道这是否是最好的方法,因为参数列表看起来很乱,感觉好像我正在创建之前已经创建过的东西。我的意思是,创建Ork对象所需的90%的信息来自其他地方。

修改

由于评论,我删除了我在敌人班级中制作的新实体,现在我有以下敌人类:

public class Enemy : Entity
{

    private string[] droplist;

    private int defaultgold;

    private string weaknesses;

    private string resistances;

    private string[] taunts;

    private string aggro;

    private string critchance;

    private string threshold;


    public string[] Droplist { get { return droplist; } set { droplist = value; } }
    public int Defaultgold { get { return defaultgold; } set { defaultgold = value; } }
    public string Weaknesses { get { return weaknesses; } set { weaknesses = value; } }
    public string Resistances { get { return resistances; } set { resistances = value; } }
    public string[] Taunts { get { return taunts; } set { taunts = value; } }
    public string Aggro { get { return aggro; } set { aggro = value; } }
    public string Critchance { get { return critchance; } set { critchance = value; } }
    public string Threshold { get { return threshold; } set { threshold = value; } }




    public void setEntity(string EnName, string EnType, string EnSpawnzone, string En_Class, int EnDefaultlevel, int EnMaxlevel, string Entrait, int Enstr, int Enagi, int Endex, int Enhel, int Enman)
    {


        setStats(Enstr, Enagi, Endex, Enhel, Enman);

        Name = EnName;
        Type = EnType;
        Spawnzone = EnSpawnzone;
        _Class = En_Class;
        Defaultlevel = EnDefaultlevel;
        Maxlevel = EnMaxlevel;
        trait = Entrait;

    }

    public Enemy(string[] Droplist, int Defaultgold, string Weaknesses, string Resistances, string[] Taunts, string Aggro, string Critchance, string Threshold) 

        :base(Name,Type,Spawnzone,_Class,Defaultlevel,Maxlevel,trait,str,agi,dex,hel,man)
    {

        //Set all the local variables to the passed in parameters.

        setEntity(Name, Type, Spawnzone, _Class, Defaultlevel, Maxlevel, trait, str, agi, dex, hel, man);

        droplist = Droplist;
        defaultgold = Defaultgold;
        weaknesses = Weaknesses;
        resistances = Resistances;
        taunts = Taunts;
        aggro = Aggro;
        critchance = Critchance;
        threshold = Threshold;

}

我的问题是,我不知道如何从Entity获取信息,我无法访问Name,Type,Spawnzone,etc,etc变量,因为他们期望对象引用。

3 个答案:

答案 0 :(得分:0)

Entity应该是Enemy扩展的基类(具有并非所有实体都具有的其他统计信息)。从Enemy的构造函数中,您应该调用父Entity构造函数。这是有道理的,因为Enemy"是(n)" Entity

然而,目前,您的设置是Enemy"有(n)" Entity,这没有任何意义。

修改

你已经拥有了这个:

public class Ork : Enemy

我说,你还需要这个:

public class Enemy : Entity

而C#我自动认为类基类构造函数,所以你不必做任何其他事情。只需对Entity构造函数中的所有Entity,Enemy中的Enemy执行所有操作,等等。

答案 1 :(得分:0)

注意:我们看到您刚刚编辑,以澄清您的Enemy课程延伸Entity。然后,我不明白为什么要在Entity构造函数中实例化Enemy类。 EntityEnemy之间的逻辑关系是什么? EnemyEntity的一种吗?或者,Enemy是否有Entity

也许您可以考虑将Entity实例传递给Enemy构造函数,以避免必须将Entity类的所有单个组件传递给Enemy构造函数。像这样:

public class Entity {
    // other code

    public Entity(string Type, string Spawnzone, string _Class, int Defaultlevel, int Maxlevel, string trait, int str, int agi, int dex, int hel, int man) {
        // move setEntity code here.
    }

    // other code
}

public class Enemy {

    // other code

    public Enemy(string[] Droplist, int Defaultgold, string Weaknesses, string Resistances, string[] Taunts, string Aggro, string Critchance, string Threshold, string Name, Entity entity) {
            this.entity = entity;

            droplist = Droplist;
            defaultgold = Defaultgold;
            weaknesses = Weaknesses;
            resistances = Resistances;
            taunts = Taunts;
            aggro = Aggro;
            critchance = Critchance;
            threshold = Threshold;
    }

    // other code
}   

答案 2 :(得分:0)

我假设您的Enemy类包含Entity的实例。在你的情况下,这是一种错误的做法。如果您的Enemy类被Entity扩展,那么您可以使用其构造函数将通用值设置为父类。就像你在HillRacing程序中完成它一样。

(确保您的Enemy班级没有Entity类型的成员。如果它是Enemy的基础

public class Entity 
{
    public string Type;
    public string Spawnzone;
    public string _Class;
    public string Defaultlevel;
    public string Maxlevel;
    public string Trait;
    public string str;
    public string agi;
    public string dex;
    public string hel;
    public string man;

    public Entity(string Type, string Spawnzone, string _Class, int Defaultlevel, int Maxlevel, string trait, int str, int agi, int dex, int hel, int man) 
    {
        this.Type = Type;
        this.Spawnzone = Spawnzone;
        this._Class = _Class;
        this.Defaultlevel = Defaultlevel;
        this.Maxlevel = Maxlevel;
        this.Trait = trait;
        this.str = str;
        this.agi = agi;
        this.dex = dex;
        this.hel = hel;
        this.man = man;
    }

}

public class Enemy 
{
    public string[] droplist;;
    public int defaultgold;
    public string weaknesses;
    public string resistances;
    public string[] taunts;
    public string aggro;
    public string critchance;
    public string threshold;


    public Enemy(string[] Droplist, int Defaultgold, string Weaknesses, string Resistances, string[] Taunts, string Aggro, string Critchance, string Threshold, string Name, string Type, string Spawnzone, string _Class, int Defaultlevel, int Maxlevel, string trait, int str, int agi, int dex, int hel, int man ) 
    : base(Type, Spawnzone, _Class, Defaultlevel, Maxlevel, trait, str, agi, dex, hel, man)
    {

        droplist = Droplist;
        defaultgold = Defaultgold;
        weaknesses = Weaknesses;
        resistances = Resistances;
        taunts = Taunts;
        aggro = Aggro;
        critchance = Critchance;
        threshold = Threshold;
    }

}