<?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; származtatás</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>Származtatás (Frissítve)</title>
		<link>http://users.atw.hu/csharpkodok/?p=52</link>
		<comments>http://users.atw.hu/csharpkodok/?p=52#comments</comments>
		<pubDate>Sat, 18 Jul 2009 05:34:58 +0000</pubDate>
		<dc:creator>nameless</dc:creator>
				<category><![CDATA[kód]]></category>
		<category><![CDATA[származtatás]]></category>

		<guid isPermaLink="false">http://users.atw.hu/csharpkodok/?p=52</guid>
		<description><![CDATA[Származtatás:


using System;
&#160;
namespace Program
{
    class Car
    {
        public readonly int maxSpeed;
        private int currSpeed;
&#160;
        public Car(int max)
        {
     [...]]]></description>
			<content:encoded><![CDATA[<p>Származtatás:<br />
<span id="more-52"></span></p>

<div class="wp_syntax"><div class="code"><pre class="language" style="font-family:monospace;">using System;
&nbsp;
namespace Program
{
    class Car
    {
        public readonly int maxSpeed;
        private int currSpeed;
&nbsp;
        public Car(int max)
        {
            maxSpeed = max;
        }
        public Car()
        {
            maxSpeed = 55;
        }
&nbsp;
        public int Speed
        {
            get { return currSpeed; }
            set
            {
                currSpeed += value;
                if (currSpeed &gt; maxSpeed)
                {
                    currSpeed = maxSpeed;
                }
            }
        }
    }
    class Minivan : Car
    {
&nbsp;
    }
    class MainClass
    {
        public static void Main()
        {
            Car myCar = new Car(292);
            myCar.Speed = 300;
            Console.WriteLine(&quot;My car is going {0} KMH&quot;, myCar.Speed);
&nbsp;
            Minivan myVan = new Minivan();
            myVan.Speed = 50;
            Console.WriteLine(&quot;Minivan speed is {0} KMH&quot;, myVan.Speed);
&nbsp;
            Console.ReadKey();
        }
    }
&nbsp;
}</pre></div></div>

<p>Ebben a példában látható, hogy a Minivan származtatott osztály hozzáfér az ősosztály minden tagjához, így neki is beállíthatjuk a sebességét&#8230;</p>
<p>Egészítsük ki még az alábbi kódot, egy rádió (lezárt) osztállyal, ami azt mutatja meg, hogy be van-e kapcsolva a rádió:</p>

<div class="wp_syntax"><div class="code"><pre class="language" style="font-family:monospace;">using System;
&nbsp;
namespace Program
{
    sealed class Radio
    {
        public void Power(bool turnOn)
        {
            Console.WriteLine(&quot;Radio on: {0}&quot;, turnOn);
        }
    }
&nbsp;
    class Car
    {
        public readonly int maxSpeed;
        private int currSpeed;
&nbsp;
        private Radio myRadio = new Radio();
        public void TurnOnRadio(bool onOff)
        {
            //Hívás továbbítása a belső objektumnak
            myRadio.Power(onOff);
        }
&nbsp;
        public Car(int max)
        {
            maxSpeed = max;
        }
        public Car()
        {
            maxSpeed = 55;
        }
&nbsp;
        public int Speed
        {
            get { return currSpeed; }
            set
            {
                currSpeed += value;
                if (currSpeed &gt; maxSpeed)
                {
                    currSpeed = maxSpeed;
                }
            }
        }
    }
    class Minivan : Car
    {
&nbsp;
    }
    class MainClass
    {
        public static void Main()
        {
            Car myCar = new Car(292);
            myCar.Speed = 300;
            myCar.TurnOnRadio(true);
            Console.WriteLine(&quot;My car is going {0} KMH&quot;, myCar.Speed);
&nbsp;
            Minivan myVan = new Minivan();
            myVan.Speed = 50;
            myVan.TurnOnRadio(false);
            Console.WriteLine(&quot;Minivan speed is {0} KMH&quot;, myVan.Speed);
&nbsp;
            Console.ReadKey();
        }
    }
&nbsp;
}</pre></div></div>

<p>Egy komplett kód, ami bemutatja a base kulcsszó használatát és a származtatás lényegét:</p>

<div class="wp_syntax"><div class="code"><pre class="language" style="font-family:monospace;">using System;
namespace Car
{
    class Auto
    {
        protected int currSpeed, currManeuverability, currBreak;
        protected string currBrand;
&nbsp;
        public Auto() { }
&nbsp;
        public Auto(int speed, int Maneuverability, int Break, string Brand)
        {
            currSpeed = speed;
            if (Maneuverability &gt; 100)
            {
                currManeuverability = 100;
            }
            else
                currManeuverability = Maneuverability;
            currBreak = Break;
            currBrand = Brand;
        }
        public virtual void DisplayInfo()
        {
            Console.WriteLine(&quot;\nAz auto markaja: {0}&quot;, currBrand);
            Console.WriteLine(&quot;Az auto sebessege: {0}km/h&quot;, currSpeed);
            Console.WriteLine(&quot;Az auto iranyithatosaga (max100): {0}&quot;, currManeuverability);
            Console.WriteLine(&quot;Az auto fekezese 100km/h-rol: {0} masodperc&quot;, currBreak);
        }
&nbsp;
    }
&nbsp;
    class SportAuto : Auto
    {
&nbsp;
        private int currSpeedAfterNitro;
        public SportAuto() { }
        public SportAuto(int speed, int Maneuverability, int Break, string Brand, int nitro)
            : base(speed, Maneuverability, Break, Brand)
        {
            currSpeedAfterNitro = speed + nitro;
        }
        public override void DisplayInfo()
        {
            base.DisplayInfo();
            Console.WriteLine(&quot;Az auto sebessege nitro kozben: {0}km/h\n&quot;, currSpeedAfterNitro);
        }
    }
&nbsp;
    class LuxusAuto : Auto
    {
        private int currBreakAfterABSon;
        public LuxusAuto() { }
        public LuxusAuto(int speed, int Maneuverability, int Break, string Brand, int ABSslow)
            : base(speed, Maneuverability, Break, Brand)
        {
            currBreakAfterABSon = Break - ABSslow;
        }
        public override void DisplayInfo()
        {
            base.DisplayInfo();
            Console.WriteLine(&quot;Az auto fekezese 100km/h-rol ABS bekapcsolasa utan: {0} masodperc\n&quot;, currBreakAfterABSon);
        }
    }
&nbsp;
    class MainClass
    {
        public static void Main()
        {
            Auto szuzuki = new Auto(180, 12, 36, &quot;Szuzuki&quot;);
            szuzuki.DisplayInfo();
&nbsp;
            Auto skoda = new Auto(198, 10, 45, &quot;Skoda&quot;);
            skoda.DisplayInfo();
&nbsp;
            Auto other_data = new Auto();
&nbsp;
            SportAuto ferrari = new SportAuto(320, 88, 7, &quot;Ferrari&quot;, 40);
            ferrari.DisplayInfo();
&nbsp;
&nbsp;
            LuxusAuto mercedes = new LuxusAuto(280, 79, 8, &quot;Mercedes&quot;, 3);
            mercedes.DisplayInfo();
&nbsp;
&nbsp;
            Console.ReadKey();
        }
    }
}</pre></div></div>

<p>Van még egy lehetőség az öröklésre&#8230; A &#8220;van egy&#8221; lehetőség: (tartalmazás és delegálás)</p>

<div class="wp_syntax"><div class="code"><pre class="language" style="font-family:monospace;">using System;
namespace program
{
    class BenfitPackage
    {
        public double ComputePayDeDuction()
        {
            return 1225.0;
        }
    }
&nbsp;
    partial class Employee
    {
        protected BenfitPackage empBenfits = new BenfitPackage();
&nbsp;
        public double GetBenfitCost()
        { return empBenfits.ComputePayDeDuction(); }
&nbsp;
        public BenfitPackage Benfits
        {
            get { return empBenfits; }
            set { empBenfits = value; }
        }
&nbsp;
        protected int currage;
        public Employee(int age)
        { currage = age; }
&nbsp;
        public virtual void DisplyInfo()
        {
            Console.WriteLine(&quot;The employee age: {0}\n&quot;, currage);
        }
    }
    class Managar : Employee
    {
        private int Mtime;
        public Managar(int age, int ManagerTime)
            : base(age)
        { Mtime = ManagerTime; }
&nbsp;
        public override void DisplyInfo()
        {
            Console.WriteLine(&quot;The Maneger age: {0}&quot;, currage);
            Console.WriteLine(&quot;He deal with Manager: {0}\n&quot;, Mtime);
        }
    }
&nbsp;
    class SalesPerson : Employee
    {
        private string SellThings;
        public SalesPerson(int age, string sell)
            : base(age)
        { SellThings = sell; }
&nbsp;
        public override void DisplyInfo()
        {
            Console.WriteLine(&quot;The Sales person age: {0}&quot;,currage);
            Console.WriteLine(&quot;He sell: {0}\n&quot;,SellThings);
        }
    }
&nbsp;
    sealed class PTSalesPerson : SalesPerson
    {
        public PTSalesPerson(int age, string sell)
            : base(age, sell)
        {}
    }
&nbsp;
    class MainClass
    {
        public static void Main()
        {
            Employee Norbert = new Employee(26);
            Norbert.DisplyInfo();
&nbsp;
            Managar Karoly = new Managar(36, 14);
            Karoly.DisplyInfo();
&nbsp;
            SalesPerson Jones = new SalesPerson(46, &quot;Bike&quot;);
            Jones.DisplyInfo();
&nbsp;
            double cost = Jones.GetBenfitCost();
            Console.WriteLine(&quot;cost: {0}&quot;, cost);
&nbsp;
            Console.ReadKey();
        }
    }
}</pre></div></div>

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