Advertisement
damesova

Class Templates

Sep 29th, 2022 (edited)
867
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.63 KB | Source Code | 0 0
  1. //Шаблонни класове
  2.  
  3. //=================================
  4.  
  5. namespace ClassTemplates
  6. {
  7.     //Шаблонен клас с един параметър:
  8.     class Alpha<X>
  9.     {
  10.         //Поле от шаблонен тип:
  11.         public X code;
  12.  
  13.         //Конструктор:
  14.         public Alpha(X a)
  15.         {
  16.             code = a;
  17.         }
  18.  
  19.         //Метод:
  20.         public void show()
  21.         {
  22.             Console.WriteLine("Field of type {0}: {1}", typeof(X).Name, code);
  23.         }
  24.     }
  25.  
  26. //======================================
  27.  
  28. namespace ClassTemplates
  29. {
  30.     //Шаблонен клас с два параметъра:
  31.     class Beta<X, Y>
  32.     {
  33.         //Полета от шаблонен тип:
  34.         public X first;
  35.         public Y second;
  36.  
  37.         //Конструктор:
  38.         public Beta(X a, Y b)
  39.         {
  40.             first = a;
  41.             second = b;
  42.         }
  43.  
  44.         //Метод:
  45.         public void show()
  46.         {
  47.             Console.WriteLine("First field type {0}: {1}", typeof(X).Name, first);
  48.             Console.WriteLine("Seconf field type {0}: {1}", typeof(Y).Name, second);
  49.  
  50.         }
  51.     }
  52. }
  53.  
  54.  
  55. //======================================
  56.  
  57. namespace ClassTemplates
  58. {
  59.     //Главен клас:
  60.     public class Program
  61.     {
  62.         //Главен метод:
  63.         static void Main(string[] args)
  64.         {
  65.             //Създаване на обекти на шаблонен клас:
  66.             Alpha<int> A = new Alpha<int>(123);
  67.             Alpha<string> B = new Alpha<string>("Obj B");
  68.             Alpha<char> C = new Alpha<char>('C');
  69.  
  70.             //Извикване на методи от обектите на шаблонния клас:
  71.             A.show();
  72.             B.show();
  73.             C.show();
  74.  
  75.             Console.WriteLine();
  76.  
  77.             //Създаване на обекти на шаблонен клас:
  78.             Beta<int, char> objA = new Beta<int, char>(123, 'A');
  79.             Beta<string, bool> objB = new Beta<string, bool>("objB", true);
  80.             Beta<char, char> objC = new Beta<char, char>('B', 'C');
  81.  
  82.             //Извикване на методи от обектите на шаблонния клас:
  83.  
  84.             objA.show();
  85.             objB.show();
  86.             objA.show();
  87.         }
  88.  
  89.     }
  90. }
  91.  
  92.  
  93. //Output:
  94. /*
  95. Field of type Int32: 123
  96. Field of type String: Obj B
  97. Field of type Char: C
  98.  
  99. First field type Int32: 123
  100. Second field type Char: A
  101. First field type String: objB
  102. Second field type Boolean: True
  103. First field type Int32: 123
  104. Second field type Char: A
  105. */
Tags: C#
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement