c# interface virtual ,abstract ,多态实例
2009-01-13 09:58
309 查看
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
ClassA a = new ClassA();
a.show();
}
private void button2_Click(object sender, EventArgs e)
{
ClassA b = new ClassB();
b.show();
}
private void button3_Click(object sender, EventArgs e)
{
ClassA c = new ClassC();
c.show();
}
private void button4_Click(object sender, EventArgs e)
{
IMath m_math = new Math();
int c=m_math.add(1, 2);
MessageBox.Show(c.ToString());
}
private void button5_Click(object sender, EventArgs e)
{
IMath m_math = new MathEx();
int c=m_math.multipy(1, 2);
MessageBox.Show(c.ToString());
}
private void button6_Click(object sender, EventArgs e)
{
IMath m_math = new MathClass();
int c=m_math.add(1, 2);
MessageBox.Show(c.ToString());
}
private void button7_Click(object sender, EventArgs e)
{
Math m_math = new MathClass();
int c=m_math.Cal(1,2);
MessageBox.Show(c.ToString());
}
}
#region 多重继承
public class ClassA
{
public virtual void show()
{
MessageBox.Show("class A show");
}
}
public class ClassB : ClassA
{
public override void show()
{
MessageBox.Show("class B show");
base.show();
}
}
public class ClassC : ClassB
{
public override void show()
{
MessageBox.Show("class C show");
return;
}
}
#endregion
#region 虚方法
//虚方法的函数
abstract public class Worker
{
abstract public void DoSomething();
public virtual void Dispaly()
{
MessageBox.Show("Worker display");
}
}
public class Manger : Worker
{
public override void DoSomething()
{
MessageBox.Show("Manger do something");
//throw new Exception("The method or operation is not implemented.");
}
public override void Dispaly()
{
MessageBox.Show("manger display");
}
}
public class Boss : Manger
{
public override void DoSomething()
{
MessageBox.Show("Boss Do something");
//base.DoSomething();
}
public override void Dispaly()
{
MessageBox.Show("Boss Display");
}
}
#endregion
#region 接口函数
public interface IMath
{
int add(int a, int b);
int multipy(int a, int b);
}
public class Math : IMath
{
public int add(int a, int b)
{
return a + b;
}
public virtual int multipy(int a, int b)
{
return a * b;
}
public virtual int Cal(int a, int b)
{
return a - b;
}
}
public class MathEx : Math
{
public override int multipy(int a, int b)
{
return a * b * a+a+b;
//return base.multipy(a, b);
}
}
public class MathClass : Math, IMath
{
public void Do()
{
MessageBox.Show("Do mathclass");
}
}
#endregion
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
ClassA a = new ClassA();
a.show();
}
private void button2_Click(object sender, EventArgs e)
{
ClassA b = new ClassB();
b.show();
}
private void button3_Click(object sender, EventArgs e)
{
ClassA c = new ClassC();
c.show();
}
private void button4_Click(object sender, EventArgs e)
{
IMath m_math = new Math();
int c=m_math.add(1, 2);
MessageBox.Show(c.ToString());
}
private void button5_Click(object sender, EventArgs e)
{
IMath m_math = new MathEx();
int c=m_math.multipy(1, 2);
MessageBox.Show(c.ToString());
}
private void button6_Click(object sender, EventArgs e)
{
IMath m_math = new MathClass();
int c=m_math.add(1, 2);
MessageBox.Show(c.ToString());
}
private void button7_Click(object sender, EventArgs e)
{
Math m_math = new MathClass();
int c=m_math.Cal(1,2);
MessageBox.Show(c.ToString());
}
}
#region 多重继承
public class ClassA
{
public virtual void show()
{
MessageBox.Show("class A show");
}
}
public class ClassB : ClassA
{
public override void show()
{
MessageBox.Show("class B show");
base.show();
}
}
public class ClassC : ClassB
{
public override void show()
{
MessageBox.Show("class C show");
return;
}
}
#endregion
#region 虚方法
//虚方法的函数
abstract public class Worker
{
abstract public void DoSomething();
public virtual void Dispaly()
{
MessageBox.Show("Worker display");
}
}
public class Manger : Worker
{
public override void DoSomething()
{
MessageBox.Show("Manger do something");
//throw new Exception("The method or operation is not implemented.");
}
public override void Dispaly()
{
MessageBox.Show("manger display");
}
}
public class Boss : Manger
{
public override void DoSomething()
{
MessageBox.Show("Boss Do something");
//base.DoSomething();
}
public override void Dispaly()
{
MessageBox.Show("Boss Display");
}
}
#endregion
#region 接口函数
public interface IMath
{
int add(int a, int b);
int multipy(int a, int b);
}
public class Math : IMath
{
public int add(int a, int b)
{
return a + b;
}
public virtual int multipy(int a, int b)
{
return a * b;
}
public virtual int Cal(int a, int b)
{
return a - b;
}
}
public class MathEx : Math
{
public override int multipy(int a, int b)
{
return a * b * a+a+b;
//return base.multipy(a, b);
}
}
public class MathClass : Math, IMath
{
public void Do()
{
MessageBox.Show("Do mathclass");
}
}
#endregion
相关文章推荐
- C#的主要 关键字 new、sealed、partial、interface、virtual、abstract、this、base、override、return、break、continue...
- c#中的interface abstract virtual override和new
- c#中的interface abstract与virtual
- c#中的interface abstract与virtual
- c#中的interface abstract virtual override和new
- C#中的interface,abstract和virtual
- C#Virtual方法(虚方法)、Abstract方法(抽象方法)、Interface的区别
- c#中的interface abstract 与 virtual
- c#中的interface abstract与virtual介绍
- c#中的interface abstract与virtual介绍
- c#中的interface abstract与virtual
- C#中的static,interface,virtual,abstract,override的用法
- C#中的interface,abstract和virtual
- c#中的interface abstract virtual
- C#中的static,interface,virtual,abstract,override的用法
- c#中的interface abstract 与 virtual(转)
- 我理解c#中的interface abstract virtual override和new
- C#的static,interface,virtual,abstract,override的区别用法
- C#的static,interface,virtual,abstract,override的区别用法
- c#中的interface abstract virtual override和new