클래스의 상속(Class inheritance)

상속이란 말을 어디선가 들어본 적이 있는 것 같지 않나요? 
짐작하는 그 상속이 맞냐구요? 네 맞습니다. 
혹시나 상속이 뭔지 들어보 적 없는 분들을 위해 무엇인지 알려드리려고 합니다. 
상속이란 네이버 지식백과를 빌어 다음과 같이 정의되어 있습니다. 
'일정한 친족적 관계가 있는 사람 사이에 한 쪽이 사망하거나 법률상의 원인이 발생하였을 때 재산적 또는 친족적 권리와 의무를 계승하는 제도'. 
즉, 부모님이 돌아가셨다고 할 때 그 유산을 자식이 물려받는 것이라고 할 수 있습니다. 
클래스의 상속도 이와 똑같습니다. 
객체 지향 프로그래밍에선 부모 클래스와 자식 클래스가 있는데, 
부모 클래스는 자식 클래스의 기반이 된다 하여 기반 클래스라고 부르기도 하고, 
자식 클래스는 부모 클래스로부터 파생되었다고 해서 파생 클래스라고도 부르기도 합니다.

C#에서, 클래스를 다른 클래스로 상속하려면 다음과 같이 클래스 이름 뒤에 콜론(:)을 추가하고 상속하려는 클래스의 이름을 덧붙이시면 됩니다.

using NoName;
using NoName.Windows.Forms;
using NoName.Timers;

class FrmMain : Form
{
NoName.Timers.Timer tm = new NoName.Timers.Timer();
FrmChild f1 = new FrmChild();

int ____width=0;
int ____height=0;
int ____idx=0;

public FrmMain()
{
this.Text="FrmMain";

tm.Elapsed += new ElapsedEventHandler(____time_tick);
tm.Interval = 1000;
tm.Start();

f1.Show();
}
void ____time_tick(object sender, ElapsedEventArgs e)
{
this.Width = f1.Width + ____width;
this.Height = f1.Height + ____height;

____width += 10;
____height += 10;

____idx++;
if(____idx==13) f1.Close();
if(____idx==14) this.Close();

this.Text = "FrmMain" + ">>>[" + ____idx.ToString("0000") + "]";
}
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
const int WM_KEYDOWN = 0x0100, WM_SYSKEYDOWN = 0x0104, WM_PAINT = 0x000f, WM_SIZE = 0x0005;

if ((msg.Msg == WM_KEYDOWN) || (msg.Msg == WM_SYSKEYDOWN))
{
         switch (keyData.ToString())
         {
case "Return" :
DialogResult dialogResult = MessageBox.Show("[" + this.Text + "]" + ">>Quit OK!!", "Inform",
              MessageBoxButtons.OKCancel, MessageBoxIcon.Information);

if (dialogResult == DialogResult.OK) Application.Exit();
break;
case "Right"  :
break;
case "Left"   :
break;
         default:
             break;
         }
     }
return base.ProcessCmdKey(ref msg, keyData);
}
protected override void WndProc(ref Message m)
{
const int WM_KEYDOWN = 0x0100, WM_SYSKEYDOWN = 0x0104, WM_PAINT = 0x000f, WM_SIZE = 0x0005;

base.WndProc(ref m);

try
{
switch(m.Msg)
{
case WM_PAINT :
break;
default:
break;
}
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}

class FrmChildBase : Form
{
//
}

class FrmChild : FrmChildBase
{
public FrmChild()
{
this.Width=400;
this.Height=400;

this.Text="FrmChild";
}
}

class Program
{
public static void Main()
{
Application.Run(new FrmMain());
}
}






+ Recent posts