Derdiedaskeks
Derdiedaskeks

Reputation: 1

C#: OOP inheritance of property

I want to inherit that a class has a property, but different implementations of this class will have different values for this property and the property should be available without instantiating an object. Eg: every animal has a value numberOfLegs. For every cat it is 4 for every snake it is 0. Now I want to loop through some animal Types and print out how many legs that animal subclass has without creating an instance of that class.

Upvotes: 0

Views: 153

Answers (3)

Carvo Loco
Carvo Loco

Reputation: 2138

I guess you could also define your interface requirement (having a numberOfLegs property) by implementing an interface instead of inheriting from a base class. That way, you can place the static values at the levels of abstraction where they don't change across instances:

interface IAnimal
{
    int numberOfLegs { get; }
}

class Snake : IAnimal
{
    public static int numberOfLegs = 0;

    int IAnimal.numberOfLegs
    {
        get { return numberOfLegs; }
    }
}

class Cat: IAnimal
{
    public static int numberOfLegs = 4;

    int IAnimal.numberOfLegs
    {
        get { return numberOfLegs; }
    }
}

Upvotes: 0

taquion
taquion

Reputation: 2767

You could give a chance to the following:

First declare your Animal abstract base class which will be responsible for storing types and numberoflegs

abstract class Animal
    {
        protected readonly static IDictionary<Type, int> _legsDictionary = new Dictionary<Type, int>();
    }

And an Animal abstract class which has the static property NumberOfLegs:

abstract class Animal<T> :Animal where T : class
    {
        public static int NumberOfLegs
        {
            get => _legsDictionary.ContainsKey(typeof(T)) ? _legsDictionary[typeof(T)] : -1;
            set
            {
                _legsDictionary[typeof(T)] = value;
            }
        }
    }

And then just declare as many Animals as you want >>>

class Cat : Animal<Cat> { }
class Snake : Animal<Snake> { }
class Human : Animal<Human> { }

And testing:

static void Main(string[] args)
        {
            Cat.NumberOfLegs = 4;
            Snake.NumberOfLegs = 0;
            Human.NumberOfLegs = 2;

            Console.WriteLine(Cat.NumberOfLegs);
            Console.WriteLine(Snake.NumberOfLegs);
            Console.WriteLine(Human.NumberOfLegs);
            Console.ReadLine();
        }

Upvotes: 1

Max Play
Max Play

Reputation: 4037

The thing you want to achieve isn't possible in the way you want it to be.

The static keyword stays persistent between every class. What you can do is the following:

class Animal
{
    public static int LegCount { get { return 0; } }
}

class Snake : Animal
{
    public static new int LegCount { get { return 0; } }
}

class Human : Animal
{
    public static new int LegCount { get { return 2; } }
}

class Cat : Animal
{
    public static new int LegCount { get { return 4; } }
}

With the new keyword you can make each new type return a different value.

But note that a List<Animal> will always always return 0, since the new only hides within the type and is not the same as an override.

Upvotes: 0

Related Questions