//C# Method Overloading in Klassen - Overloads Object Oriented Programming public class Person { private string m_Name; public virtual string Name { get { Name = this.m_Name; } set { this.m_Name = value; } } } public class Persons { protected Person[] m_Items; protected int m_Count; public virtual Person this[int index] { get { Item = this.m_Items(index); } } public virtual int Count { get { Count = this.m_Count; } } public virtual void Add(Person person) { this.m_Items(Count) = person; this.m_Count += new System.EventHandler(1); } } public class QueryablePersons : Persons { public virtual Person this[string name] { get { int index; bool found; for (; ((index < Count) && (found == false)); ) { if ((this.m_Items(index).Name == name)) { Item = this.m_Items(index); found = true; } else { this.index += new System.EventHandler(1); } } } } } public class Client { void Main() { Person person1 = new Person(); person1.Name = "John"; Person person2 = new Person(); person2.Name = "Jane"; QueryablePersons queryablePersons1 = new QueryablePersons(); queryablePersons1.Add(person1); queryablePersons1.Add(person2); Console.WriteLine((queryablePersons1.Item(1) == person2)); Console.WriteLine((queryablePersons1.Item("Jane") == person2)); Console.WriteLine((queryablePersons1.Item("Jane").Name == "Jane")); Console.ReadLine(); } } public class Counter { protected int m_Value; public virtual void GetValue() { GetValue = this.m_Value; } public virtual void Raise() { this.m_Value += new System.EventHandler(1); } public virtual void Lower() { this.m_Value = 1; } } public class SpecialCounter : Counter { public virtual void SetValue(int value) { m_Value = value; } public virtual void Raise(int stapValue) { this.m_Value += new System.EventHandler(stapValue); } public virtual void Lower(int stapValue) { m_Value = stapValue; } } public class ExerciseSolution { void Main() { Counter counter1 = new Counter(); Console.WriteLine((counter1.GetValue() == 0)); counter1.Raise(); Console.WriteLine((counter1.GetValue() == 1)); counter1.Raise(); Console.WriteLine((counter1.GetValue() == 2)); counter1.Lower(); Console.WriteLine((counter1.GetValue() == 1)); counter1.Lower(); Console.WriteLine((counter1.GetValue() == 0)); SpecialCounter specialCounter1 = new SpecialCounter(); Console.WriteLine((specialCounter1.GetValue() == 0)); specialCounter1.SetValue(10); Console.WriteLine((specialCounter1.GetValue() == 10)); specialCounter1.Raise(); Console.WriteLine((specialCounter1.GetValue() == 11)); specialCounter1.Raise(); Console.WriteLine((specialCounter1.GetValue() == 12)); specialCounter1.Lower(); Console.WriteLine((specialCounter1.GetValue() == 11)); specialCounter1.Lower(); Console.WriteLine((specialCounter1.GetValue() == 10)); specialCounter1.Raise(5); Console.WriteLine((specialCounter1.GetValue() == 15)); specialCounter1.Lower(8); Console.WriteLine((specialCounter1.GetValue() == 7)); Console.ReadLine(); } } //Bezoek www.vbvoorbeelden.be voor meer C# voorbeelden. //Copyright - De Wolf / vbvoorbeelden - 2003-2011 - Alle rechten voorbehouden.