Results 1 to 3 of 3
  1. #1
    Join Date
    Feb 2012
    Posts
    31

    Subclassing in C#

    Hiya

    I just wanted to properly know if it's possible to use subclassing in C#.

    I know its the colon sign like this code shown here:

    Code:
    class person : gender{
    
    }
    But this is the real issue I am having trouble with here shown in JavaScript
    (btw I never use JS before so apologies if I get this wrong):

    Code:
    class person{
    
    var name : string;
    var age: int;
    
    }
    
    class gender extends person{
    var isMale : boolean;
    
    function B(name : string, age: int, isMale : boolean){
    
    super.name = name;
    super.age = age;
    this.isMale = isMale;
    
           }
    }
    Now I want to convert this JavaScript class into C# but I do not properly know how to do it.

  2. #2
    mantragora Guest
    It's very straightforward. What you want is called inheritance in C# world.

    Code:
    class Person
    {
        public string Name { get; set; }
        public int Age { get; set; }
    }
    
    class Gender : Person
    {
        public bool IsMale { get; set; }
    
        public Gender(string name, int age, bool ismale)
        {
            Name = name;
            Age = age;
            IsMale = ismale;
        }
    }
    or if you know that you will not instantiate person class alone, just use it as base, you can make it abstract and expand from it:

    Code:
    abstract class Person
    {
        public string Name { get; set; }
        public int Age { get; set; }
    
        protected Person(string name, int age)
        {
            Name = name;
            Age = age;
        }
    }
    
    class Gender : Person
    {
        public bool IsMale { get; set; }
    
        public Gender(string name, int age, bool ismale) :base(name, age)
        {
            IsMale = ismale;
        }
    }
    Or just use interface instead of base class

    Code:
    interface IPerson
    {
        string Name { get; set; }
        int Age { get; set; }
    }
    
    class Gender : IPerson
    {
        public Gender(string name, int age, bool ismale)
        {
            Name = name;
            Age = age;
            IsMale = ismale;
        }
    
        public string Name { get; set; }
        public int Age { get; set; }
        public bool IsMale { get; set; }
    }
    or you can use composition instead of inheritance
    Code:
    sealed class Person
    {
        public string Name { get; set; }
        public int Age { get; set; }
    
        public Person(string name, int age)
        {
            Name = name;
            Age = age;
        }
    }
    
    class Gender
    {
        private readonly Person _person;
    
        public Gender(string name, int age, bool ismale)
        {
            _person = new Person(name, age);
            IsMale = ismale;
        }
    
        public string Name { get { return _person.Name; } }
        public int Age { get { return _person.Age; } }
    
        public bool IsMale { get; set; }
    }
    EDIT:
    Remember that properties don't work in Unity if you want to add parameter field to your asset. You need public variable, not property like I used here.
    Last edited by mantragora; 04-28-2012 at 07:43 AM.

  3. #3
    Join Date
    Feb 2012
    Posts
    31
    Awesome stuff thank you so much mantra.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •