@willmotil wrote:
This is a example for reference. This is so that i can just link to it as a reply to similar questions. Feel free to expand on it as you like.
This is a basic console example.
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace InheiritanceAndBaseDerivation { class Program { static void Main(string[] args) { AnimalLover me = new AnimalLover(); me.Draw(); Console.ReadLine(); } } public class Animal { public string objectTypesName = "Animal"; public string noise = "..."; public virtual string Speak { get { return noise; } set { noise = value; } } public virtual void Update() { } public virtual void Draw() { Console.WriteLine(objectTypesName + " " + Speak); } } public class Cat : Animal { public Cat() { objectTypesName = "Cat"; Speak = "Meow"; } } public class Dog : Animal { public Dog() { objectTypesName = "Dog"; Speak = "Ruff"; } public override void Draw() { // because dogs like to speak alot Console.WriteLine(objectTypesName + " " + Speak + " " + Speak); } } public class Lizard : Animal { public Lizard() { objectTypesName = "Lizard"; } } public class AnimalLover { List<Animal> animals = new List<Animal>(); public AnimalLover() { animals.Add(new Animal()); animals.Add(new Cat()); animals.Add(new Dog()); animals.Add(new Lizard()); } public void Update() { } public void Draw() { foreach (Animal item in animals) { item.Draw(); } } } // Output as follows // // Animal ... // Cat Meow // Dog Ruff Ruff // Lizard ... // }
Posts: 1
Participants: 1