c# interface virtual ,abstract ,多态实例

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 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值