Lập trình hướng đối tượng – Tính đa hình (Polymorphism)

class Class1
{

static void Main(string[] args)
{

TestOOP obj = new TestOOP();
Window w = new Window();
obj.Add(w);

TextBox tb = new TextBox();
obj.Add(tb);

Button bt = new Button();
obj.Add(bt);

Label lbl = new Label();
obj.Add(lbl);

obj.Do();
Console.ReadLine();

}

}
class Window
{

public virtual void Draw()
{

Console.WriteLine(“Windows”);

}

}

class TextBox:Window
{

public override void Draw()
{

Console.WriteLine(“TextBox”);

}

}
class Button:Window
{

public override void Draw()
{

Console.WriteLine(“Button”);

}

}
class Label:Window
{

public override void Draw()
{

Console.WriteLine(“Label”);

}

}
class TestOOP
{

private ArrayList arr = new ArrayList();
public void Add(Window w)
{

arr.Add(w);

}
public void Do()
{

foreach(Window w in arr)

{

w.Draw();

}

}

}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s