Konstruktorhívások láncolása a this kulcsszóval

Filed Under (kód) by nameless on 15-07-2009

Tagged Under :

Konstruktorhívások láncolása a this kulcsszóval:

using System;
 
namespace Program
{
    class MainClass
    {
        public static void Main()
        {
 
            Motorcycle m = new Motorcycle(5,"Daniel");
            Console.WriteLine(m.driverIntensity); Console.WriteLine(m.driverName);
            Console.ReadKey();
        }
    }
    class Motorcycle
    {
        public int driverIntensity;
        public string driverName;
 
        public Motorcycle() {}
 
        public Motorcycle(int intensity)
        {
            SetIntensity(intensity);
        }
        public Motorcycle(int intensity, string name)
        {
            SetIntensity(intensity);
            driverName = name;
        }
        public void SetIntensity(int intensity)
        {
            if (intensity > 10)
            {
                intensity = 10;
            }
            driverIntensity = intensity;
        }
    }
}
using System;
 
namespace Program
{
    class MainClass
    {
        public static void Main()
        {
 
            Motorcycle m = new Motorcycle(5);
            Console.WriteLine(m.driverIntensity); Console.WriteLine(m.driverName);
            Console.ReadKey();
        }
    }
    class Motorcycle
    {
        public int driverIntensity;
        public string driverName;
 
        //konstruktor láncolása
        public Motorcycle() { }
        public Motorcycle(int intesity) : this(intesity, "Nincs nev megadva...") { }
        public Motorcycle(string name) : this(0, name) { }
 
        //Ez a fő konstruktor, ez végzi a tényleges munkát...
        public Motorcycle(int intesity, string name)
        {
            if (intesity > 10)
            {
                intesity = 10;
            }
            driverIntensity = intesity;
            driverName = name;
        }
    }
}

Magyarázat:

using System;
 
namespace Program
{
  sealed class Terbed
  {
      string favourite;
 
      public Terbed()
      {
          Console.WriteLine("sima konstruktor");
      }
 
      public Terbed(string favourite)
          : this()
      {
          this.favourite = favourite;
      }
  }
 
  class MainClass
  {
      public static void Main()
      {
          new Terbed("programming in C#");
 
          Console.ReadKey();
      }
  }
}

Van a Terbed osztály. Csinálhatok új Terbed-et simán, meg úgy, hogy adok neki kedvenc időtöltést.
Utóbbi esetben kell egy plusz paraméter, de a sima konstruktort is meg kell hívni.
Első esetben nincs kedvenc időtöltés, ezért marad null a favourite, nem nyúlunk hozzá fölöslegesen.

4.0-van vandefault paraméter, ilyesmi:

public Terbed(string favourite = "")
{
  this.favourite = favourite;
  Console.WriteLine("sima konstruktor");
}

Ez kényelmes, mert csak egy konstruktor kell, viszont fölöslegesen kap értéket újra a favourite változó.

Egy egyszerűbb példa:

using System;
 
namespace Program
{
    class MainClass
    {
        public static void Main()
        {
 
            new EntryPoint("I Love C#");
            new EntryPoint();
            Console.ReadKey();
        }
    }
    class EntryPoint
    {
        public EntryPoint()
        {
            Console.WriteLine("Sima konstruktor");
        }
        public EntryPoint(string favourite)
            : this()
        {
            Console.WriteLine(favourite);
        }
    }
}

Post a comment