XNA Creators Club Online
Page 1 of 1 (7 items)
Sort Posts: Previous Next

Inheritance - Constants - different game objects

Last post 9/5/2009 9:45 AM by Idefix. 6 replies.
  • 9/4/2009 6:16 PM

    Inheritance - Constants - different game objects

    This might be more of a C# question - but I;m trying to make multiple game objects that have different characteristics.


    Class Ball
    {
        Private const radius = 5;

        private Create()
        {
            MakeBall(radius)
        }


    Class Tennisball:Ball
    {
        private const "override" radius = 2;
    }


    How can i make classes that just differ on some values? I know i can initalize them through the constructor, or set them as properties afterwards.. but that's not really neat.


  • 9/4/2009 6:20 PM In reply to

    Re: Inheritance - Constants - different game objects

    using the constructor method would be the neatest. There are two ways I can think about doing this:

    abstract Class Ball
    {
    private float Radius { get { return GetRadius(); } }
    private abstract float GetRadius();
    }

    or

    class Ball
    {
    public float Radius {get; protected set; }
    }

    The second is preferable to me, you just have to set it in the constructor.
    Regards,
    Louis Ingenthron
    Fortis Venaliter
    Lead Developer of FV ProductionsFV Productions
  • 9/4/2009 6:32 PM In reply to

    Re: Inheritance - Constants - different game objects

    Answer
    Reply Quote
    Another possibility is:

    Class Ball  
    {  
        private float radius;  
     
        protected Ball(float radius)  
        {  
            this.radius = radius;  
        }  
        ...  
    }  
     
    Class TennisBall : Ball  
    {  
        public TennisBall() : Ball(5.0f)  
        {  
        }  
        ...  



  • 9/4/2009 7:39 PM In reply to

    Re: Inheritance - Constants - different game objects

    Another possibility...

    class Ball  
    {  
        public virtual float Radius  
        {  
            get { return 1.0f; }  
        }  
    }  
     
    class TennisBall : Ball  
    {  
        public override float Radius  
        {  
            get { return 5.0f; }  
        }  

    Lots of ways to do the same thing.  It all comes down to code design and what serves you best in terms of efficiency and reusability.
  • 9/4/2009 8:05 PM In reply to

    Re: Inheritance - Constants - different game objects

    If the only difference between the class and its child is the numbers or the values of other properties you should be using instances instead.

    Ball tennisBall = new Ball(5f); 
    Ball pingPongBall = new Ball(1f); 
    Ball basketBall = new Ball(25f); 

    The overrides keyword is for when you want to rework the way the code is implemented: for example some proto-ball whose radius is a function of its weight instead of a set value.

    public override float Radius 
        get { return this.Weight * 10f; } 

  • 9/4/2009 8:11 PM In reply to

    Re: Inheritance - Constants - different game objects

    MrLeebo:
    If the only difference between the class and its child is the numbers or the values of other properties you should be using instances instead.

    Ball tennisBall = new Ball(5f); 
    Ball pingPongBall = new Ball(1f); 
    Ball basketBall = new Ball(25f); 

    The overrides keyword is for when you want to rework the way the code is implemented: for example some proto-ball whose radius is a function of its weight instead of a set value.

    public override float Radius 
        get { return this.Weight * 10f; } 



    That makes good sense, thanks for the heads up. :)
  • 9/5/2009 9:45 AM In reply to

    Re: Inheritance - Constants - different game objects

    David Hunt:
    Another possibility is:

    Class Ball  
    {  
        private float radius;  
     
        protected Ball(float radius)  
        {  
            this.radius = radius;  
        }  
        ...  
    }  
     
    Class TennisBall : Ball  
    {  
        public TennisBall() : Ball(5.0f)  
        {  
        }  
        ...  





    I like this - didn;t thought of it yet; So i can just "set" all the properties , inside the child class, and how they are used is all in the base class, and automatically and invisible.;
Page 1 of 1 (7 items) Previous Next
var gDomain='m.webtrends.com'; var gDcsId='dcschd84w10000w4lw9hcqmsz_8n3x'; var gTrackEvents=1; var gFpc='WT_FPC'; /*<\/scr"+"ipt>");} /*]]>*/
DCSIMG