CSharp examples for Custom Type:Constructor
Call Derived Class Constructors
using System;/* w ww . java2 s .c o m*/ class Car { private string brandName; public Car(string initialBrandName) { brandName = initialBrandName; } public Car() { brandName = "unknown"; } public string BrandName { get { return brandName; } set { brandName = value; } } } class RacingCar : Car { private string engineName; public RacingCar() : base() { engineName = "unknown"; } public RacingCar(string initialBrandName, string e) : base(initialBrandName) { engineName = e; } public string OnBoardCameraName { get { return engineName; } set { engineName = value; } } } class CarTester { public static void Main() { Car myNoNameCar = new Car(); Car myCar = new Car("Volvo"); RacingCar myNoNameRacingCar = new RacingCar(); RacingCar yourRacingCar = new RacingCar("Ferrari", "Sony"); Console.WriteLine("The name of myNoNameCar: " + myNoNameCar.BrandName); Console.WriteLine("The name of myCar: " + myCar.BrandName); Console.WriteLine("The name of myNoNameRacingCar: " + myNoNameRacingCar.BrandName); Console.WriteLine("The camera name of myNoNameRacingCar: " + myNoNameRacingCar.OnBoardCameraName); Console.WriteLine("The name of yourRacingCar: " + yourRacingCar.BrandName); Console.WriteLine("The camera name of yourRacingCar: " + yourRacingCar.OnBoardCameraName); } }