Advertisement
Guest User

Tree.cs

a guest
Oct 17th, 2019
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.23 KB | None | 0 0
  1. using System;
  2. namespace OOP_3Laba
  3. {
  4.     public class Tree
  5.     {
  6.         public int age;
  7.         public string name;
  8.         public int height;
  9.         public static int NumberOfObjects;
  10.  
  11.         //readonly поле
  12.         public readonly int ReadOnlyField;
  13.        
  14.         //Конструктор по умолчанию
  15.         public Tree()
  16.         {
  17.             age = 17;
  18.             name = "OldOak";
  19.             height = 300;
  20.             NumberOfObjects++;
  21.         }
  22.  
  23.         //Конструктор с параметрами
  24.         public Tree(string _name, int _age, int _height)
  25.         {
  26.             name = _name;
  27.             age = _age;
  28.             height = _height;
  29.             NumberOfObjects++;
  30.         }
  31.  
  32.         //Статический конструктор
  33.         static Tree()
  34.         {
  35.          
  36.         }
  37.  
  38.         //Конструктор копирования
  39.         public Tree(Tree obj)
  40.         {
  41.             name = obj.name;
  42.             age = obj.age;
  43.             height = obj.height;
  44.         }
  45.  
  46.         //Деструктор
  47.         //Закоментирован, т.к удаляет все объекты Tree
  48.         //~Tree()
  49.         //{
  50.         //    Console.WriteLine("Объект уничтожен");
  51.         //}
  52.  
  53.         //Классический get и set
  54.         public int SetTreeAge
  55.         {
  56.             get
  57.             {
  58.                 return age;
  59.             }
  60.  
  61.             set
  62.             {
  63.                 if (value < 1)
  64.                     age = 1;
  65.                 else if (value > 500)
  66.                     age = 500;
  67.                 else
  68.                     age = value;
  69.             }
  70.         }
  71.         //Автосвойство
  72.         public int AutoSetAge
  73.         { get; set; } = 1;
  74.  
  75.  
  76.         //Метод GetInfo
  77.         public void GetInfo()
  78.         {
  79.             Console.WriteLine($"Имя:{name}  Возраст:{age} Высота:{height}");
  80.            
  81.         }
  82.  
  83.         //Переопределение
  84.         public override int GetHashCode()
  85.         {
  86.             return base.GetHashCode();
  87.         }
  88.  
  89.         public override string ToString()
  90.         {
  91.             return base.ToString();
  92.         }
  93.  
  94.  
  95.     }
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement