c#实现多态主要有3种方法,虚方法,抽象类,接口

1 虚方法

在父类的方法前面加关键字virtual, 子类重写该方法时在方法名前面加上override关键字,例如下面的person类的sayhello方法

class person
{
  public person(string name)
  {
    this.name = name;
  }

  string _name;

  public string name { get => _name; set => _name = value; }

  //父类方法加virtual,子类用override重写该方法,就实现了多态 
  public virtual void sayhello()  
  {
    console.writeline("我是父类的方法");
  }
}

学生类和教师都继承于person

class student : person
{
  public student(string name) : base(name) { }

  public override void sayhello()
  {
    console.writeline("我叫{0}, 我是学生", this.name);
  }
}

class teacher:person
{
  public teacher(string name) : base(name) { }

  public override void sayhello()
  {
    console.writeline("我叫{0}, 我是老师", this.name);
  }
}

然后在main函数中使用多态

student st = new student("李雷");
teacher th = new teacher("井边君");

person[] p = { st, th }; //子类对象赋给父类

for(int i = 0; i < p.length; i++)
{
  p[i].sayhello();
}

console.readkey();

本例全部代码

using system;

namespace 多态之虚方法
{
  class program
  {
    static void main(string[] args)
    {
      student st = new student("李雷");
      teacher th = new teacher("井边君");

      person[] p = { st, th }; //子类对象赋给父类

      for(int i = 0; i < p.length; i++)
      {
        p[i].sayhello();
      }

      console.readkey();
    }
  }

  class person
  {
    public person(string name)
    {
      this.name = name;
    }

    string _name;

    public string name { get => _name; set => _name = value; }

    //父类方法加virtual,子类用override重写该方法,就实现了多态 
    public virtual void sayhello()  
    {
      console.writeline("我是父类的方法");
    }
  }

  class student : person
  {
    public student(string name) : base(name) { }

    public override void sayhello()
    {
      console.writeline("我叫{0}, 我是学生", this.name);
    }
  }

  class teacher:person
  {
    public teacher(string name) : base(name) { }

    public override void sayhello()
    {
      console.writeline("我叫{0}, 我是老师", this.name);
    }
  }
}

2 抽象类

在类前面加关键字abstract,方法前面加abstract,抽象方法不能有函数体。

抽象类的特点:

(1)可以有字段

(2)可以有非抽象方法

抽象类实现多态的代码如下

using system;
using system.collections.generic;

namespace 多态之抽象类
{
  abstract class person
  {
    //抽象方法不能有函数体
    public abstract void sayhello();
  }

  class student : person
  {
    public override void sayhello()
    {
      console.writeline("我是子类student重写的抽象方法"); ;
    }
  }

  class teacher : person
  {
    public override void sayhello()
    {
      console.writeline("我是子类teacher重写的抽象方法"); ;
    }
  }

  class program
  {
    static void main(string[] args)
    {
      list<person> clist = new list<person>();

      student st = new student();
      teacher th = new teacher();
      clist.add(st);
      clist.add(th);

      foreach(person p in clist)
      {
        p.sayhello();
      }

      console.readkey();
    }
  }
}

3 接口实现多态

代码如下

using system;
using system.collections.generic;

namespace 多态之接口
{
  public interface iweapon
  {
    void fire();
  }

  class gun : iweapon
  {
    public void fire()
    {
      console.writeline("我是枪");
    }
  }

  class sword : iweapon
  {
    public void fire()
    {
      console.writeline("我是剑");
    }

  }

  class tank : iweapon
  {
    public void fire()
    {
      console.writeline("我是坦克");
    }
  }

  class program
  {
    static void main(string[] args)
    {
      list<iweapon> list = new list<iweapon>();

      gun gun = new gun();
      sword sw = new sword();
      tank ta = new tank();
      list.add(gun);
      list.add(sw);
      list.add(ta);

      foreach (iweapon p in list)
      {
        p.fire();
      }

      console.readkey();
    }
  }
}

接口的特点

(1)接口里的方法不能有修饰符,默认是public

(2)接口的方法不能有函数体

(3)接口中不能包含示例字段,不能有构造函数

(4)接口里的方法被继承的类重写时,不需要用override关键字,接口不能被实例化

(5)接口之间可以继承,并且可以多继承;接口不能继承于类,但是类可以继承于接口

(6)一个类可以同时继承一个类,并实现多个接口

到此这篇关于c#多态的三种实现方式(小结)的文章就介绍到这了,更多相关c#多态实现内容请搜索www.887551.com以前的文章或继续浏览下面的相关文章希望大家以后多多支持www.887551.com!