<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>C# kódok &#187; konstruktorhívások láncolása</title>
	<atom:link href="http://users.atw.hu/csharpkodok/" rel="self" type="application/rss+xml" />
	<link>http://users.atw.hu/csharpkodok</link>
	<description>Csharp kódok gyüjteménye</description>
	<lastBuildDate>Fri, 07 Aug 2009 11:44:01 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Konstruktorhívások láncolása a this kulcsszóval</title>
		<link>http://users.atw.hu/csharpkodok/?p=7</link>
		<comments>http://users.atw.hu/csharpkodok/?p=7#comments</comments>
		<pubDate>Wed, 15 Jul 2009 12:13:57 +0000</pubDate>
		<dc:creator>nameless</dc:creator>
				<category><![CDATA[kód]]></category>
		<category><![CDATA[konstruktorhívások láncolása]]></category>

		<guid isPermaLink="false">http://users.atw.hu/csharpkodok/?p=7</guid>
		<description><![CDATA[Konstruktorhívások láncolása a this kulcsszóval:


using System;
&#160;
namespace Program
{
    class MainClass
    {
        public static void Main()
        {
&#160;
            Motorcycle m = new Motorcycle(5,&#34;Daniel&#34;);
     [...]]]></description>
			<content:encoded><![CDATA[<p>Konstruktorhívások láncolása a this kulcsszóval:</p>
<p><span id="more-7"></span></p>

<div class="wp_syntax"><div class="code"><pre class="language" style="font-family:monospace;">using System;
&nbsp;
namespace Program
{
    class MainClass
    {
        public static void Main()
        {
&nbsp;
            Motorcycle m = new Motorcycle(5,&quot;Daniel&quot;);
            Console.WriteLine(m.driverIntensity); Console.WriteLine(m.driverName);
            Console.ReadKey();
        }
    }
    class Motorcycle
    {
        public int driverIntensity;
        public string driverName;
&nbsp;
        public Motorcycle() {}
&nbsp;
        public Motorcycle(int intensity)
        {
            SetIntensity(intensity);
        }
        public Motorcycle(int intensity, string name)
        {
            SetIntensity(intensity);
            driverName = name;
        }
        public void SetIntensity(int intensity)
        {
            if (intensity &gt; 10)
            {
                intensity = 10;
            }
            driverIntensity = intensity;
        }
    }
}</pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="language" style="font-family:monospace;">using System;
&nbsp;
namespace Program
{
    class MainClass
    {
        public static void Main()
        {
&nbsp;
            Motorcycle m = new Motorcycle(5);
            Console.WriteLine(m.driverIntensity); Console.WriteLine(m.driverName);
            Console.ReadKey();
        }
    }
    class Motorcycle
    {
        public int driverIntensity;
        public string driverName;
&nbsp;
        //konstruktor láncolása
        public Motorcycle() { }
        public Motorcycle(int intesity) : this(intesity, &quot;Nincs nev megadva...&quot;) { }
        public Motorcycle(string name) : this(0, name) { }
&nbsp;
        //Ez a fő konstruktor, ez végzi a tényleges munkát...
        public Motorcycle(int intesity, string name)
        {
            if (intesity &gt; 10)
            {
                intesity = 10;
            }
            driverIntensity = intesity;
            driverName = name;
        }
    }
}</pre></div></div>

<p><strong>Magyarázat:</strong></p>

<div class="wp_syntax"><div class="code"><pre class="language" style="font-family:monospace;">using System;
&nbsp;
namespace Program
{
  sealed class Terbed
  {
      string favourite;
&nbsp;
      public Terbed()
      {
          Console.WriteLine(&quot;sima konstruktor&quot;);
      }
&nbsp;
      public Terbed(string favourite)
          : this()
      {
          this.favourite = favourite;
      }
  }
&nbsp;
  class MainClass
  {
      public static void Main()
      {
          new Terbed(&quot;programming in C#&quot;);
&nbsp;
          Console.ReadKey();
      }
  }
}</pre></div></div>

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

<div class="wp_syntax"><div class="code"><pre class="language" style="font-family:monospace;">public Terbed(string favourite = &quot;&quot;)
{
  this.favourite = favourite;
  Console.WriteLine(&quot;sima konstruktor&quot;);
}</pre></div></div>

<p>Ez kényelmes, mert csak egy konstruktor kell, viszont fölöslegesen kap értéket újra a favourite változó.</p>
<p><strong>Egy egyszerűbb példa:</strong></p>

<div class="wp_syntax"><div class="code"><pre class="language" style="font-family:monospace;">using System;
&nbsp;
namespace Program
{
    class MainClass
    {
        public static void Main()
        {
&nbsp;
            new EntryPoint(&quot;I Love C#&quot;);
            new EntryPoint();
            Console.ReadKey();
        }
    }
    class EntryPoint
    {
        public EntryPoint()
        {
            Console.WriteLine(&quot;Sima konstruktor&quot;);
        }
        public EntryPoint(string favourite)
            : this()
        {
            Console.WriteLine(favourite);
        }
    }
}</pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://users.atw.hu/csharpkodok/?feed=rss2&amp;p=7</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
