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
2009/Study 모임 B조2009. 2. 21. 15:04
abstract(추상화) 
:    예를 들어 나는 말을 한다. 하지만 말은 하지만 어떤 말을 할 지는 알 수 없다.
     이런 명확하지 않지만 반드시 하는 것을 정의 할때 추상화 class를 사용 합니다.
    (개인적으로 생각 하는 개념 )                                     

   
[형식]
 지정자 abstract  class 이름 :  기반 abstract
{
   // 멤버 목록
}



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

namespace AbstractExam
{

    public class Ok
    {
}
    public abstract class A : Ok
    {
        public abstract string Name { get; set; }
        public abstract void Display();
        public abstract void Move();
    }

    public class B : A
    { 
       public override string Name
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }
        public override void Display()
        {
            Console.WriteLine("화면에 보여준다.");
        }
        public override void Move()
        {
            Console.WriteLine("이동");
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            B ok = new B();

            ok.Display();
            ok.Move();
        }
    }
}


Posted by penguindori