C# da Base Komutu
Kalıtım almış class’larda ana sınıftan bir methodu veya değişkeni çağırmak amaçlı kullanılır.
namespace CLASSLAR
{
class bir
{
public int x;
public bir(int x)
{
this.x = x;
}
}
class iki:bir
{
public int y;
public iki(int x,int y):base(x)
{
this.y = y;
}
}
class uc:iki
{
public int z;
public uc(int x,int y, int z):base(x,y)
{
this.z= z;
}
}
}
private void button1_Click(object sender, EventArgs e)
{
int s1, s2, s3;
s1 = Convert.ToInt32(textBox1.Text);
s2 = Convert.ToInt32(textBox2.Text);
s3 = Convert.ToInt32(textBox3.Text);
uc g = new uc(s1, s2, s3);
this.Text = g.x + ” ” + g.y + ” ” + g.z;
iki g1 = new iki(s1*2, s2*2);
this.Text = g1.x + ” ” + g1.y;
bir g3 = new bir(s1*3);
this.Text = g3.x.ToString() ;
}
Private , Public , Protacted , Internal
Private: Bir methodun private bildirisiyle deklare edilmesi, o methodun sadece o class’ın içerisinde kullanılabileceğini gösterir.
Protected: Class içinde ve miras verdiği (türetilmiş) sınıfta kullanılabilir.
Public: Class, yeni nesne oluşturulduğunda, türetilmiş sınıfta ve DLL uzantılı dosyalar içerisinden ulaşılabilir.
Internal:Public gibidir.Farkı;başka namespacelerden çağırılamıyor olmasıdır
namespace CLASSLAR
{
class Class4
{
public int a;
private int b;
protected int c;
internal int d;
public void goster()
{
a = 1;
b = 1;
c = 1;
d = 1;
}
}
class türetilmis : Class4
{
public void turetilmis_goster()
{
a = 1;
// b = 2; private tanımla türetilmiş sınıfta kullanılamaz
c = 3;
d = 4;
}
}
}
private void button1_Click(object sender, EventArgs e)
{
Class4 yeni = new Class4();
yeni.a = 4;
this.Text = yeni.a.ToString();
yeni.d = 2;
button1.Text = yeni.d.ToString();
}
private void button2_Click(object sender, EventArgs e)
{
türetilmis y = new türetilmis();
y.a = 5;
y.d = 6;
}
Kalıtım (Miras)
Bir class’ın, başka bir class’ın barındırdığı kodları daha geliştirerek yeni methodlar oluşturmasına kalıtım olayı denir.
class Class2
{
public int sonuc;
public int kare(int sayi)
{
sonuc = sayi * sayi;
return sonuc;
}
public void goster(int deger)
{
int veri;
veri = this.kare(deger);
MessageBox.Show(“Sayının Karesi= ” + veri.ToString());
}
}
class yeni : Class2
{
public int kup(int sayi)
{
return sayi * sayi * sayi;
}
}
private void button1_Click(object sender, EventArgs e)
{
yeni a = new yeni();
this.Text = a.kare(Convert.ToInt32(textBox1.Text)) + ” ” + a.kup(Convert.ToInt32(textBox1.Text));
}
