Advertisement
WilleMahMille

Exempel - Polymorfi Abstract

Nov 8th, 2019
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.92 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 Dog());
  13.             animals.Add(new Cat());
  14.             for(int i = 0; i < animals.Count; i++) {
  15.                Console.WriteLine(animals[i].Sound());
  16.             }
  17.             Console.ReadKey(true);
  18.         }
  19.     }
  20.     public abstract class Animal {
  21.         public abstract string Sound();
  22.     }
  23.     public class Cat : Animal {
  24.         public Cat() { }
  25.         public override string Sound() {
  26.             return ("Mjau");
  27.         }
  28.     }
  29.     public class Dog : Animal {
  30.         public Dog() { }
  31.         public override string Sound() {
  32.             return ("Vov");
  33.         }
  34.     }
  35.  
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement