2009/Study 모임 B조2009. 2. 21. 21:02
예제 1)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace InheritanceMethodExam
{
    class BTeam
    {
        protected string TeamName;
        protected int Age;
        public BTeam(string TeamName, int Age) //생성자
        {
            this.TeamName = TeamName;
            this.Age = Age;
        }

        public virtual void BteamInfo() //virtual 은 상속 받는 class에서 변경 하겠다 선언
        {
            Console.WriteLine(TeamName);
            Console.WriteLine(Age);
        }
    }
    class BTeamInfo : BTeam
    {
        protected string address;
        public BTeamInfo(string TeamName, int Age, string address)
            : base(TeamName, Age) // 부모의 생성자를 호출하여 초기화
        {
            this.address = address;
        }
        public override void BteamInfo()
        {
            base.BteamInfo();
            Console.WriteLine(address);
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            BTeamInfo bt = new BTeamInfo("박세우",28,"경기 안산");
            bt.BteamInfo();
        }
    }
}

예제2) 부모 class는 자식 class를 모두 받을 수 있다.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace InheritanceMethod2
{
    class A { }
    class B : A { }
    class C : A { }
    class D : A { }
   
    class Program
    {
        static void Main(string[] args)
        {
            A[] As = new A[10];  //부모 class A에 자식 클레스 대입이 가능 합니다.
            As[0] = new B();
            As[1] = new C();
            As[2] = new D();
        }
    }
}


Posted by penguindori