Advertisement
Geralt1001

zad 6.5

Oct 23rd, 2016
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.50 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 Mwzad65
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             Base a = new Base();
  14.             a.Method();
  15.  
  16.             Base der = new Derived1();
  17.             der.Method();
  18.  
  19.             Basee derr = new Derived2();
  20.             derr.Method();
  21.  
  22.  
  23.             Console.ReadKey();
  24.         }
  25.  
  26.  
  27.         public class Base
  28.         {
  29.             /// <summary>
  30.             /// gdyby nie była wirtualna, to przy utworzenu obiektu zadziała polimorfizm statyczny (Base der = new Derived1();)
  31.             /// czyli zadziała metoda Method z Base zamiast z Derived1
  32.             /// </summary>
  33.             virtual public void Method()
  34.             {
  35.                 Console.WriteLine("baza");
  36.             }
  37.         }
  38.  
  39.         abstract public class Basee
  40.         {
  41.             /// <summary>
  42.             /// możemy też użyć metody abstrakcyjnej, ale ta nie moze mieć żadnego ciała metody
  43.             /// </summary>
  44.             abstract public void Method();
  45.         }
  46.  
  47.         public class Derived1: Base
  48.         {
  49.             override public void Method()
  50.             {
  51.                 Console.WriteLine("111111111");
  52.             }
  53.         }
  54.  
  55.         public class Derived2 : Basee
  56.         {
  57.             override public void Method()
  58.             {
  59.                 Console.WriteLine("222222222");
  60.             }
  61.         }
  62.     }
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement