Advertisement
WilleMahMille

Exempel - Polymorfi Virtual

Nov 8th, 2019
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.93 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace Polymorfi {
  8.     class Program {
  9.         static void Main(string[] args) {
  10.             List<Animal> animals = new List<Animal>();
  11.             animals.Add(new Dog());
  12.             animals.Add(new Cat());
  13.             animals.Add(new Animal());
  14.             for(int i = 0; i < animals.Count; i++) {
  15.                Console.WriteLine(animals[i].Sound());
  16.             }
  17.             Console.ReadKey(true);
  18.         }
  19.     }
  20.     public class Animal {
  21.         public Animal() { }
  22.         public virtual string Sound() {
  23.             return ("Argh");
  24.         }
  25.     }
  26.     public class Cat : Animal {
  27.         public override string Sound() {
  28.             return ("Mjau");
  29.         }
  30.     }
  31.     public class Dog : Animal {
  32.         public override string Sound() {
  33.             return ("Vov");
  34.         }
  35.     }
  36.  
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement