//C# Implementatie Object Oriented Programming namespace Example1 { public class Product { private decimal m_Price; private decimal m_TaxPercentage; private decimal m_PriceIncludingTax; public virtual decimal Price { get { Price = this.m_Price; } set { this.m_Price = value; this.setPriceIncludingTax(); } } public virtual decimal TaxPercentage { get { TaxPercentage = this.m_TaxPercentage; } set { this.m_TaxPercentage = value; this.setPriceIncludingTax(); } } public virtual void GetPriceIncludingTax() { GetPriceIncludingTax = this.m_PriceIncludingTax; } private void setPriceIncludingTax() { this.m_PriceIncludingTax = (Price * (1 + (TaxPercentage / 100))); } } public class Client { void Main() { Product product1 = new Product(); product1.Price = 100; product1.TaxPercentage = 8; Console.WriteLine(product1.GetPriceIncludingTax()); Console.ReadLine(); } } } namespace Example2 { public class Product { private decimal m_Price; private decimal m_TaxPercentage; private decimal m_PriceIncludingTax; public virtual decimal Price { get { Price = this.m_Price; } set { this.m_Price = value; this.setPriceIncludingTax(); } } public virtual decimal TaxPercentage { get { TaxPercentage = this.m_TaxPercentage; } set { this.m_TaxPercentage = value; this.setPriceIncludingTax(); } } public virtual decimal PriceIncludingTax { get { PriceIncludingTax = this.m_PriceIncludingTax; } } private void setPriceIncludingTax() { this.m_PriceIncludingTax = (Price * (1 + (TaxPercentage / 100))); } } public class Client { void Main() { Product product1 = new Product(); product1.Price = 100; product1.TaxPercentage = 8; Console.WriteLine(product1.PriceIncludingTax); Console.ReadLine(); } } } namespace Exercise1 { public class Addition { private int m_Value1; private int m_Value2; public virtual int Value1 { get { Value1 = this.m_Value1; } set { this.m_Value1 = value; } } public virtual int Value2 { get { Value2 = this.m_Value2; } set { this.m_Value2 = value; } } public virtual void GetResult() { GetResult = (Value1 + Value2); } } public class AdditionTestFixture { void Main() { Addition addition1 = new Addition(); Console.WriteLine((addition1.Value1 == 0)); Console.WriteLine((addition1.Value2 == 0)); Console.WriteLine((addition1.GetResult() == 0)); addition1.Value1 = 1; Console.WriteLine((addition1.Value1 == 1)); Console.WriteLine((addition1.Value2 == 0)); Console.WriteLine((addition1.GetResult() == 1)); addition1.Value2 = 2; Console.WriteLine((addition1.Value1 == 1)); Console.WriteLine((addition1.Value2 == 2)); Console.WriteLine((addition1.GetResult() == 3)); Console.ReadLine(); } } } namespace Exercise2 { public class Lamp { private bool m_GivesLight; public virtual bool GivesLight { get { GivesLight = this.m_GivesLight; } } public virtual void PressButton() { this.m_GivesLight = (this.m_GivesLight == false); } } public class LampTestFixture { void Main() { Lamp lamp1 = new Lamp(); Console.WriteLine((lamp1.GivesLight == false)); lamp1.PressButton(); Console.WriteLine((lamp1.GivesLight == true)); lamp1.PressButton(); Console.WriteLine((lamp1.GivesLight == false)); Console.ReadLine(); } } } namespace Exercise3 { public class Counter { private int m_Value; private int m_StepValue = 1; public virtual int Value { get { Value = this.m_Value; } set { this.m_Value = value; } } public virtual int StepValue { get { StepValue = this.m_StepValue; } set { this.m_StepValue = value; } } public virtual void Raise() { this.Value += new System.EventHandler(StepValue); } public virtual void Lower() { Value = StepValue; } } public class CounterTestFixture { void Main() { Counter counter1 = new Counter(); Console.WriteLine((counter1.Value == 0)); Console.WriteLine((counter1.StepValue == 1)); counter1.Value = 10; Console.WriteLine((counter1.Value == 10)); Console.WriteLine((counter1.StepValue == 1)); counter1.StepValue = 5; Console.WriteLine((counter1.Value == 10)); Console.WriteLine((counter1.StepValue == 5)); counter1.Raise(); Console.WriteLine((counter1.Value == 15)); Console.WriteLine((counter1.StepValue == 5)); counter1.Lower(); Console.WriteLine((counter1.Value == 10)); Console.WriteLine((counter1.StepValue == 5)); Console.ReadLine(); } } } namespace Exercise4 { public class Robot { private int m_X; private int m_Y; private int m_Direction; public virtual int X { get { X = this.m_X; } } public virtual int Y { get { Y = this.m_Y; } } public virtual void Rotate() { this.m_Direction += new System.EventHandler(1); if ((this.m_Direction > 3)) { this.m_Direction = 0; } } public virtual void PlaceStep() { object _switch1 = this.m_Direction; if (_switch1.Equals(0)) { this.m_Y += new System.EventHandler(1); } else { if (_switch1.Equals(1)) { this.m_X += new System.EventHandler(1); } else { if (_switch1.Equals(2)) { this.m_Y = 1; } else { if (_switch1.Equals(3)) { this.m_X = 1; } } } } } } public class RobotTestFixture { public virtual void Main() { Robot robot1 = new Robot(); Console.WriteLine((robot1.X == 0)); Console.WriteLine((robot1.Y == 0)); robot1.PlaceStep(); Console.WriteLine((robot1.X == 0)); Console.WriteLine((robot1.Y == 1)); robot1.PlaceStep(); Console.WriteLine((robot1.X == 0)); Console.WriteLine((robot1.Y == 2)); robot1.Rotate(); Console.WriteLine((robot1.X == 0)); Console.WriteLine((robot1.Y == 2)); robot1.PlaceStep(); Console.WriteLine((robot1.X == 1)); Console.WriteLine((robot1.Y == 2)); robot1.Rotate(); robot1.PlaceStep(); Console.WriteLine((robot1.X == 1)); Console.WriteLine((robot1.Y == 1)); robot1.Rotate(); robot1.PlaceStep(); Console.WriteLine((robot1.X == 0)); Console.WriteLine((robot1.Y == 1)); robot1.Rotate(); robot1.PlaceStep(); Console.WriteLine((robot1.X == 0)); Console.WriteLine((robot1.Y == 2)); Console.ReadLine(); } } //Bezoek www.vbvoorbeelden.be voor meer C# voorbeelden. //Copyright - De Wolf / vbvoorbeelden - 2003-2011 - Alle rechten voorbehouden.