Çok Biçimlilik (Polymorphism) C# Ders 54
Daha fazla bilgi için : www.gorselprogramlama.com
Çok Biçimlilik (Polymorphism): Bir class’ın özelliklerinin başka bir classa aktarılmasıdır.
Örnek: İlk olarak toplama işlemini class yöntemiyle hazırlayalım.

[code language=”csharp”]
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace cok_bicimlilik
{//Daha fazla bilgi için : www.gorselprogramlama.com
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void topla_Click(object sender, EventArgs e)
{
sinif1 a = new sinif1();
label4.Text= a.topla(int.Parse(textBox1.Text),int.Parse(textBox2.Text)).ToString();
}
}}
class sinif1
{
public int topla(int a, int b)
{//Daha fazla bilgi için : www.gorselprogramlama.com
int sonuc;
sonuc = a + b;
return sonuc;
}
}
[/code]

Örnek: Yukarıdaki örnekte hazırladığımız sinif1 classının tüm özelliklerini taşıyacak sinif2 classını oluşturalım. Eğer bir classın tüm özellik metotlarını başka bir classa aktarmak istiyorsak ; class1:class2 —>Bu ifadeyle class2 nin tüm metotlarını ve özelliklerini class1’e aktarmış oluruz.

[code language=”csharp”]
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace cok_bicimlilik
{
public partial class Form1 : Form
{//Daha fazla bilgi için : www.gorselprogramlama.com
public Form1()
{
InitializeComponent();
}
private void topla_Click(object sender, EventArgs e)
{
sinif1 a = new sinif1();
label4.Text= a.topla(int.Parse(textBox1.Text),int.Parse(textBox2.Text)).ToString();
}
private void topla2_Click(object sender, EventArgs e)
{
sinif2 b=new sinif2();
label4.Text = b.topla(int.Parse(textBox1.Text), int.Parse(textBox2.Text)).ToString();
}
}
}
class sinif1
{
public int topla(int a, int b)
{
int sonuc;
sonuc = a + b;
return sonuc;
}//Daha fazla bilgi için : www.gorselprogramlama.com
}
class sinif2 : sinif1
{
}
[/code]

Not : sinif1’in tüm özelliklerini sinif2’ye aktardık.Eğer istersek sinif2 içinde kendisine has metot veya özellik oluşturabiliriz.Tabiki sinif2 içinde oluşturduğumuz metot veya özellikler sinif1’i etkilemez.Ama sinif1 içinde bir özellik veya metot eklersek bunu sinif2 içindede kullanabiliriz.
Örnek : Şimdi sinif2 içinde çıkarma işlemi yapan bir metot hazırlayalım.Bu hazırlayacağımız metodu sadece sinif2 classı içinde kullanabiliriz.

[code language=”csharp”]
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace cok_bicimlilik
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void topla_Click(object sender, EventArgs e)
{
sinif1 a = new sinif1();
label4.Text= a.topla(int.Parse(textBox1.Text),int.Parse(textBox2.Text)).ToString();
}
private void topla2_Click(object sender, EventArgs e)
{
sinif2 b=new sinif2();
label4.Text = b.topla(int.Parse(textBox1.Text), int.Parse(textBox2.Text)).ToString();
}
private void cikar_Click(object sender, EventArgs e)
{
sinif2 c =new sinif2();
label4.Text=c.cikar(int.Parse(textBox1.Text), int.Parse(textBox2.Text)).ToString();
}
}
}
class sinif1
{
public int topla(int a, int b)
{
int sonuc;
sonuc = a + b;
return sonuc;
}
}
class sinif2 : sinif1
{
public int cikar(int a, int b)
{
int sonuc;
sonuc = a – b;
return sonuc;
}
}
[/code]

Örnek: Şimdi sinif1 içinde çarpma işlemi yapan metot oluşturalım.sinif1’de oluşturulan bu metodun sinif2’de de kullanabileceğimizi örneklerle görelim.İlk olarak sinif1 içinde çağıralım çarpma işlemi metodunu.

[code language=”csharp”]
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace cok_bicimlilik
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void topla_Click(object sender, EventArgs e)
{
sinif1 a = new sinif1();
label4.Text= a.topla(int.Parse(textBox1.Text),int.Parse(textBox2.Text)).ToString();
}
private void topla2_Click(object sender, EventArgs e)
{
sinif2 b=new sinif2();
label4.Text = b.topla(int.Parse(textBox1.Text), int.Parse(textBox2.Text)).ToString();
}
private void cikar_Click(object sender, EventArgs e)
{
sinif2 c =new sinif2();
label4.Text=c.cikar(int.Parse(textBox1.Text), int.Parse(textBox2.Text)).ToString();
}
private void carp_Click(object sender, EventArgs e)
{
sinif1 d = new sinif1();
label4.Text = d.carp(int.Parse(textBox1.Text), int.Parse(textBox2.Text)).ToString();
}
}
}
class sinif1
{
public int topla(int a, int b)
{
int sonuc;
sonuc = a + b;
return sonuc;
}//Daha fazla bilgi için : www.gorselprogramlama.com
public int carp(int a, int b)
{
int sonuc;
sonuc = a * b;
return sonuc;
}
}
class sinif2 : sinif1
{
public int cikar(int a, int b)
{
int sonuc;
sonuc = a – b;
return sonuc;
}
}
[/code]
Örnek: Şimdi de sinif2 içinden çağıralım çarpma işlemi metodunu.

[code language=”csharp”]using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace cok_bicimlilik
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}//Daha fazla bilgi için : www.gorselprogramlama.com
private void topla_Click(object sender, EventArgs e)
{
sinif1 a = new sinif1();
label4.Text= a.topla(int.Parse(textBox1.Text),int.Parse(textBox2.Text)).ToString();
}
private void topla2_Click(object sender, EventArgs e)
{
sinif2 b=new sinif2();
label4.Text = b.topla(int.Parse(textBox1.Text), int.Parse(textBox2.Text)).ToString();
}
private void cikar_Click(object sender, EventArgs e)
{
sinif2 c =new sinif2();
label4.Text=c.cikar(int.Parse(textBox1.Text), int.Parse(textBox2.Text)).ToString();
}
private void carp_Click(object sender, EventArgs e)
{
sinif1 d = new sinif1();
label4.Text = d.carp(int.Parse(textBox1.Text), int.Parse(textBox2.Text)).ToString();
}
private void carp2_Click(object sender, EventArgs e)
{
sinif2 k = new sinif2();
label4.Text = k.carp(int.Parse(textBox1.Text), int.Parse(textBox2.Text)).ToString();
}
}
}
class sinif1
{
public int topla(int a, int b)
{
int sonuc;
sonuc = a + b;
return sonuc;
}
public int carp(int a, int b)
{
int sonuc;
sonuc = a * b;
return sonuc;
}//Daha fazla bilgi için : www.gorselprogramlama.com
}
class sinif2 : sinif1
{
public int cikar(int a, int b)
{
int sonuc;
sonuc = a – b;
return sonuc;
}
}
[/code]

Daha fazla bilgi için : www.gorselprogramlama.com
elinize sağlık çok güzel çalışma.
en alttaki örnekte topla ve çarp butonları 2 tane.
bunu biraz açıklarmısınız. bir tane olsa aynı görevi yapmaz mı.
tabiki bu örnek için bir tane yeterli.Ben konuya açıklık getirmek için örnek verdim.Zaten ikinci topla ve çarp butonu çok biçimlilik(sinif2:sinif1) sayesinde türeme yöntemiyle oluşturuluyor.
açıklaman için sağol.
benim anlamadigim kisim miras alma ile cok bicimlilik arasindaki fark. bana ayni gibi geliyor.
Sitemizin Google’da Ön Sıralarda Çıkması İçin Lütfen Google+ Butonuna Tıklayınız , Sayfamızı Facebookta Beğeniniz veya yazıları Twitter,Facebookta paylaşınız.