c# polymorphism
class Animal{
public virtual void animalSound() {
Console.WriteLine("The animal makes a sound");
}
}
class Pig: Animal {
public override void animalSound() {//override superclass method
Console.WriteLine("The pig says: wee wee");
}
}
class Dog: Animal {
public override void animalSound() {//override superclass method
Console.WriteLine("The dog says: bow wow");
}
}
static class main {
static void Main(){
//You can add all objects inherited from Animal to an Animal type list
Animal[] animals = new Animal[3]; //Creating the Animal List
animals[0] = new Animal(); //Add a new animal object to the list
animals[1] = new Dog(); //Add a new dog object to the list
animals[2] = new Pig(); //Add a pig object to the list
foreach (Animal a in animals){
a.animalSound(); //This statement prints the correct string no matter the class
}
}
}
Also in C#:
- get random value from list c#
- open tcp socket c#
- how to copy one array value to another without reference c#
- c# bitmap to array byte
- What is a class in c#
- unity onclick addlistener
- C# extend array
- c# static review
- c# repeat string x times
- c# skip following code in loop
- HashSet C# append
- use enter key unity
- dontdestroyonload unity
- when do i need to end a sentence with ; in c#
- asp.net list all files in folder
- c# find duplicates in list of strings
- unity3d raycast
- position eines fenster ändern visual studio
- how to get joypad axis input unity
- wpf datatrigger enum binding
- c# add to array
- c list add element
- c# singleton
- Missing compiler required member 'Microsoft.CSharp.RuntimeBinder.CSharpArgumentInfo.Create'