get/set örneği

Sınıflarda get/set kullanımı…class kisi
{
private string ad;
private string soyad;
private int bakiye;
public string Ad{get {return ad;} set { ad=value;}}
public string Soyad { get { return soyad; } set { soyad = value; } }
public int Bakiye
{
get
{
return bakiye;
}
set
{
if (value > 10)
bakiye = value;
else
bakiye = 0;
}
}
public void yaz()
{
Console.WriteLine(“Kişi ad={0} Soyad={1} Bakiye={2}”,ad,soyad,bakiye);
}

}
class Program
{
static void Main(string[] args)
{
kisi nesne=new kisi();
nesne.Ad=Console.ReadLine();
nesne.Soyad=Console.ReadLine();
nesne.Bakiye=Convert.ToInt16(Console.ReadLine());
nesne.yaz();

}
}

admin