SHARE
TWEET

c#-Polymorphism

MartinJanevski Mar 26th, 2019 97 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. namespace CSharpPolymorphism
  6. {
  7.     class Animal{
  8.     public string name;
  9.     public string sound;
  10.     public Animal(string name, string sound)
  11.     {
  12.         this.name=name;
  13.         this.sound=sound;
  14.     }
  15.     public virtual string OldMcDonald(){
  16.         return null;
  17.     }
  18.    
  19.    
  20.     }
  21.     class Dog : Animal {
  22.         public Dog(string name,string sound) : base(name,sound){
  23.            
  24.         }
  25.         public override string OldMcDonald(){
  26.             var OldMcDonaldLyrics=$@"
  27.                Old MACDONALD had a farm
  28.                E-I-E-I-O
  29.                And on his farm he had a {name}
  30.                E-I-E-I-O
  31.                With a {sound} {sound} here
  32.                And a {sound} {sound} there
  33.                Here a {sound}, there a {sound}
  34.                Everywhere a {sound} {sound}
  35.                Old MacDonald had a farm
  36.                E-I-E-I-O";
  37.                 return OldMcDonaldLyrics;
  38.         }
  39.  
  40.        
  41.     }
  42.    class Cat : Animal {
  43.         public Cat(string name,string sound) : base(name,sound){
  44.            
  45.         }
  46.         public override string OldMcDonald(){
  47.             var OldMcDonaldLyrics=$@"
  48.                Old MACDONALD had a farm
  49.                E-I-E-I-O
  50.                And on his farm he had a {name}
  51.                E-I-E-I-O
  52.                With a {sound} {sound} here
  53.                And a {sound} {sound} there
  54.                Here a {sound}, there a {sound}
  55.                Everywhere a {sound} {sound}
  56.                Old MacDonald had a farm
  57.                E-I-E-I-O";
  58.                 return OldMcDonaldLyrics;
  59.         }
  60.  
  61.        
  62.     }    class CSharpPolymorphism
  63.     {
  64.         static void Main(string[] args)
  65.         {
  66.             var dog =new Dog("dog", "bark");
  67.             var cat=new Cat("cat", "meow");
  68.            
  69.            
  70.             Console.WriteLine( dog.OldMcDonald());
  71.             Console.WriteLine();
  72.             Console.WriteLine( cat.OldMcDonald());
  73.                     }
  74.     }
  75. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top