1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
class Program { static void Main(string[] args) { Customer customer = new Customer(); customer.FirstName = "Emre"; // Customer, Persondan kalıtım aldığından persondaki propertyleri alabilecektir. customer.City = "İstanbul"; Person[] persons = new Person[2] { new Customer{ FirstName="CustomerName" }, new Student{ FirstName="StudentName" } }; foreach (var person in persons) { Console.WriteLine(person.FirstName); } } } class Person { public int Id { get; set; } public string FirstName { get; set; } public string LastName { get; set; } } class Customer : Person { public string City { get; set; } } class Student : Person { } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
using System; class Kedi { protected int ayak_sayisi = 4; //protected: Sadece bundan türetilen sınıfta görülür. public void Av_Yakala() { Console.WriteLine(“kedi sınıfı avı yakaladı.”); } } class Kaplan:Kedi //Kalıtım işlemi bu şekilde gerçekleştiriliyor diyebiliriz. Kaplan //Türeyen Kedi ise Türetilendir. Kaplana Derived Classta diyebiliriz. //Kediye ise Base Class yada Temel Class { public static void Main() { Kaplan k = new Kaplan(); //Neden instance Kedi değilde Kaplan çünkü kalıtım //bu yüzden var :) Kalıtım yaptığımızda Kaplan yazarak Kediye //böyle erişebiliriz. k.Av_Yakala(); Console.WriteLine(“Ayak Sayısı:{0}”, k.ayak_sayisi); } |