pborawski

C# pola statyczne / settery / gettery / indekser nr2

Mar 5th, 2012
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.87 KB | None | 0 0
  1. using System;
  2.  
  3. namespace Laborki_nr_2
  4. {
  5.    
  6.     class Program
  7.     {
  8.         public static void Main(string[] args)
  9.         {
  10.             Kot k = new Kot();
  11.             Kot.waga = 40; // nie moge ustawic wagi dla danego kota bo pole jest wspolne ( statyczne )
  12.             k.WstawImie("Bonifacy");
  13.             Console.WriteLine(k.GetImie());
  14.             // Get i Set za kompocu kompleksu
  15.             k.Il_wasow = 17; // odwoluje sie do kompleksu a nie bezposrednio do pola il
  16.             Console.WriteLine(k.Il_wasow); // tak samo
  17.             // Nowy Setter i Getter
  18.             k.Dlg_ogona = 5;
  19.             Console.WriteLine(k.Dlg_ogona);
  20.             // Tablica Kociarnia
  21.             Kociarnia koty = new Kociarnia();
  22.             Console.WriteLine(koty.ile());
  23.            
  24.             Console.ReadKey(true);
  25.         }
  26.     }
  27.  
  28.     //------------------
  29.     class Kot
  30.     {
  31.         public const int il_nog = 4;
  32.         public static int waga; // W C# static jest wspolny
  33.        
  34.         private string imie;
  35.         public string GetImie()
  36.         {
  37.             return imie;
  38.         }
  39.         public void WstawImie(string imiee)
  40.         {
  41.             imie = imiee;
  42.         }
  43.        
  44.         private int il_wasow;
  45.         // Tutaj musi byc inna nazwa niz pola prywatnego, wyglada na deklaracje nowej
  46.         // zmiennej czyli bez nawiasow kwadratowych. To kompleks getterow i setterow.
  47.         public int Il_wasow                            
  48.         {
  49.             get
  50.             {
  51.                 return il_wasow;
  52.             }
  53.             set
  54.             {
  55.                 if (il_wasow < 0)
  56.                     il_wasow = 0;
  57.                 else
  58.                     il_wasow = value; // musi byc value
  59.  
  60.             }
  61.         }
  62.         public double Dlg_ogona{ set; get; }
  63.         /* To wyzej wygenteruje to:
  64.          * private int dlg_ogona
  65.          * public int Dlg_ogona                            
  66.            {
  67.            get
  68.            {
  69.                 return dlg_ogona;
  70.            }
  71.            set
  72.            {
  73.                 if (dlg_ogona < 0)
  74.                     dlg_ogona = 0;
  75.                 else
  76.                     dlg_ogona = value; // musi byc value
  77.  
  78.            }
  79.            }*/
  80.     }
  81.     class Kociarnia
  82.     {
  83.         private Kot[] ko = new Kot[100]; // Tworze tablice obiektow classy kot
  84.         public Kot this[int nr] // nr  to numer indeksu | przyklad indeksera z get i set
  85.         {
  86.             get
  87.             {
  88.                 if(nr > 0 && nr < 100)
  89.                     return ko[nr];
  90.                 else
  91.                     return new Kot();
  92.             }
  93.             set
  94.             {
  95.                 ko[nr].Dlg_ogona = value;
  96.                 ko[nr].Il_wasow = value;
  97.                 ko[nr].WstawImie(value);
  98.             }
  99.  
  100.  
  101.         public int ile()
  102.         {
  103.             return ko.Length;
  104.         }
  105.     }
  106. }
Advertisement
Add Comment
Please, Sign In to add comment