Cassimus

Zwierzęta z if

Sep 27th, 2025
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.56 KB | None | 0 0
  1. // Klasa zwierząt
  2. // Kury
  3. // Krowy
  4.  
  5. public abstract class Animal
  6. {
  7.     public virtual void Eat()
  8.     {
  9.         Console.WriteLine("Zwierze je");
  10.     }
  11.  
  12.     public abstract void TakeSound();
  13. }
  14.  
  15. public class Cow : Animal
  16. {
  17.     public override void TakeSound()
  18.     {
  19.         System.Console.WriteLine("Krowa robi muuu");
  20.     }
  21.  
  22.     public override void Eat()
  23.     {
  24.         System.Console.WriteLine("Krowa je trawę");
  25.     }
  26. }
  27.  
  28. public class Hen : Animal, IFly
  29. {
  30.     public void Fly()
  31.     {
  32.         System.Console.WriteLine("kura lata");
  33.     }
  34.  
  35.     public override void TakeSound()
  36.     {
  37.         System.Console.WriteLine("kura robi ko ko");
  38.     }
  39. }
  40.  
  41. public class Program
  42. {
  43.     public static void Main()
  44.     {
  45.         Animal[] animals = new Animal[4];
  46.  
  47.         Cow mucka = new Cow();
  48.         Cow klarabella = new Cow();
  49.  
  50.         Hen kwoka = new Hen();
  51.         Hen hen1 = new Hen();
  52.  
  53.         animals[0] = mucka;
  54.         animals[1] = klarabella;
  55.         animals[2] = kwoka;
  56.         animals[3] = hen1;
  57.  
  58.         foreach (var animal in animals)
  59.         {
  60.             animal.TakeSound();
  61.             animal.Eat();
  62.             if (animal is IFly fly)
  63.             {
  64.                 fly.Fly();
  65.             }
  66.         }
  67.     }
  68. }
  69.  
  70. public interface IFly
  71. {
  72.     void Fly();
  73. }
  74.  
  75.  
  76.             //Osoba
  77.     //Uczeń        // Nauczyciel             //Trener
  78. //                    wykonaj ćwiczenie()    wykonaj ćwiczenie()  
  79.  
  80.  
  81.  
  82. //        PanStefan : Nauczyciel, Trener
  83.  
  84. //          panStefan.wykonaj ćwiczenie()
  85.  
  86. //Interface
  87.  
  88.  
  89.  
  90.  
  91.  
Advertisement
Add Comment
Please, Sign In to add comment