Advertisement
Guest User

Untitled

a guest
Aug 18th, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.25 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace Dziedziczenie_II
  7. {
  8.     class Program
  9.     {
  10.         static void Main(string[] args)
  11.         {
  12.             MyClass mc = new MyClass("test");
  13.             MyOtherClass moc = new MyOtherClass();
  14.             Test.PrintCounterA(mc);
  15.             Test.PrintCounterB(mc);
  16.             ClassA a = new ClassA();
  17.             ClassB b = new ClassB();
  18.             Test.PrintCounterA(mc);
  19.             Test.PrintCounterB(mc);
  20.             Test.PrintName(mc);
  21.             Test.PrintName(moc);
  22.             MyClass.DestroyObject(mc);
  23.             Test.PrintCounterA(mc);
  24.             Test.PrintCounterB(mc);
  25.             MyClass.DestroyObject(moc);
  26.             mc.PrintCounter(typeof(ClassA));
  27.             mc.PrintCounter(typeof(ClassB));
  28.             mc.PrintCounter(typeof(string));
  29.             mc.Print(2);
  30.             (mc.Create("new MyClass") as MyClass).Print();
  31.             System.Console.Out.WriteLine(mc == ((MyClass)mc.Create("test")));
  32.  
  33.  
  34.  
  35.         }
  36.     }
  37.  
  38.  
  39.  
  40.  
  41.  
  42.     interface InterfaceA
  43.     {
  44.         int Counter { get; }
  45.     }
  46.  
  47.     interface InterfaceB
  48.     {
  49.         int Counter { get; }
  50.     }
  51.  
  52.  
  53.     class ClassA
  54.     {
  55.         protected static int counter;
  56.  
  57.         public ClassA()
  58.         {
  59.             counter++;
  60.         }
  61.  
  62.         virtual public string GetMyName()
  63.         {
  64.             return "ClassA";
  65.         }
  66.     }
  67.  
  68.     class ClassB : ClassA
  69.     {
  70.         new protected static int counter;
  71.         protected string text;
  72.  
  73.         public ClassB()
  74.         {
  75.             counter++;
  76.             text = "";
  77.         }
  78.  
  79.         protected ClassB(string text)
  80.         {
  81.             counter++;
  82.             this.text = text;
  83.         }
  84.  
  85.         virtual public Object Create(string text)
  86.         {
  87.             return new ClassB(text);
  88.         }
  89.     }
  90.  
  91.  
  92.     class Test
  93.     {
  94.         public static void PrintCounterA(InterfaceA a)
  95.         {
  96.             System.Console.Out.WriteLine(a.Counter);
  97.         }
  98.  
  99.         public static void PrintCounterB(InterfaceB b)
  100.         {
  101.             System.Console.Out.WriteLine(b.Counter);
  102.         }
  103.  
  104.         public static void PrintName(ClassA a)
  105.         {
  106.             System.Console.Out.WriteLine(a.GetMyName());
  107.         }
  108.     }
  109.  
  110.     class MyClass : ClassB, InterfaceA, InterfaceB
  111.     {
  112.         public MyClass(string GLADos)
  113.         {
  114.             this.text = GLADos;
  115.         }
  116.  
  117.         public void PrintCounter(Type type)
  118.         {
  119.             if (type.IsAssignableFrom(typeof(ClassA)))
  120.             {
  121.                 System.Console.Out.WriteLine(ClassA.counter);
  122.                 System.Console.Out.WriteLine("\n");
  123.             }
  124.             else
  125.             {
  126.                 if (type.IsAssignableFrom(typeof(ClassB)))
  127.                 {
  128.                     System.Console.Out.WriteLine(ClassB.counter);
  129.                     System.Console.Out.WriteLine("\n");
  130.                 }
  131.                 else
  132.                 {
  133.                     System.Console.Out.WriteLine("NULL");
  134.                 }
  135.             }
  136.         }
  137.  
  138.  
  139.         public override string GetMyName()
  140.         {
  141.             Console.WriteLine(GetMyName());
  142.             Console.WriteLine("MyClass");
  143.             Console.WriteLine(text);
  144.             return (GetMyName() + "MyClass" + text);
  145.         }
  146.  
  147.         public static void DestroyObject(Object o)
  148.         {
  149.             if (o == (typeof(ClassA)))
  150.             {
  151.                 counter--;
  152.             }
  153.             else
  154.                 counter--;
  155.         }
  156.  
  157.  
  158.         public string Print()
  159.         {
  160.  
  161.             return (GetMyName());
  162.  
  163.         }
  164.  
  165.  
  166.         public void Print(int caroline)
  167.         {
  168.             int i = 0;
  169.             while (i < caroline)
  170.             {
  171.                 Console.WriteLine(GetMyName());
  172.             }
  173.  
  174.  
  175.  
  176.  
  177.             // get{return ClassB.counter}
  178.  
  179.         }
  180.  
  181.         int InterfaceA.Counter
  182.         {
  183.             get { return ClassA.counter; }
  184.         }
  185.  
  186.  
  187.         int InterfaceB.Counter
  188.         {
  189.             get { return ClassB.counter; }
  190.         }
  191.  
  192.  
  193.     }
  194.  
  195.     class MyOtherClass : ClassA
  196.     {
  197.  
  198.         string GetMyName()
  199.         {
  200.             return (GetMyName() + "MyOtherClass");
  201.         }
  202.  
  203.     }
  204.  
  205. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement