C#面向对象核心-多态

1 认识多态

1.1 基本概念

多态是同一个行为具有多个不同表现形式或形态的能力,意味着有多重形式。在面向对象编程范式中,多态性往往表现为"一个接口,多个功能"。

在 C# 中,每个类型都是多态的,因为包括用户定义类型在内的所有类型都继承自 Object。

多态性分为静态的和动态多态。在静态多态性中,函数的响应是在编译时发生的。在动态多态性中,函数的响应是在运行时发生的。.

  • 静态多态:函数重载、运算符重载

  • 动态多态:vob(virtual 虚函数,override 重写,base 父类)、抽象函数、接口

1.2 使用


 
  class GameObject
  {
  public string name;
  public GameObject(string name)
  {
  this.name = name;
  }
  //虚函数 可以被子类重写
  public virtual void Atk()
  {
  Console.WriteLine("游戏对象进行攻击");
  }
  }
  class Player : GameObject
  {
  public Player(string name) : base(name)//调用父类的构造函数
  {
   
  }
  //重写虚函数
  public override void Atk()
  {
  Console.WriteLine("玩家对象进行攻击");
  }
  }
  class Monster : GameObject
  {
  public Monster(string name) : base(name)
  {
   
  }
  public override void Atk()
  {
  base.Atk();//base代表父类,可以通过它来保留父类的行为,会调用一次父类的方法
  Console.WriteLine("怪物对象进行攻击");
  }
  }
   
  class Father
  {
  public void SpeakName()
  {
  Console.WriteLine("Father的方法");
  }
  }
  class Son : Father
  {
  public new void SpeakName()
  {
  Console.WriteLine("Son的方法");
  }
  }
   
  //Main
  //用父类取装载子类的对象时,有两个同名的方法,会优先调用父类的 多态就用来解决这类问题
  Father f = new Son();
  f.SpeakName();
  (f as Son).SpeakName();
   
  GameObject p = new Player("abc");//虚函数和重写解决了问题
  p.Atk();
   
  GameObject m = new Monster("def");
  m.Atk();
  /*
  输出:
  Father的方法
  Son的方法
   
  玩家对象进行攻击
   
  游戏对象进行攻击
  怪物对象进行攻击
  */

2 abstract 抽象类和抽象方法

2.1 抽象类

用 abstract 关键字修饰的类。

特点:

  • 不能被实例化

  • 可以包含抽象方法

  • 继承抽象类必须重写其抽象方法


 
  abstract class Thing//抽象一类物品
  {
  public string name;
   
  //可以在抽象类中写抽象函数
  }
  class Water : Thing
  {
   
  }

2.2 抽象方法

用 abstract 修饰的方法,又叫 纯虚方法

特点:

  • 只能在抽象类中声明

  • 没有方法体

  • 不能是私有的

  • 继承后必须实现,用override重写


 
  abstract class Fruits
  {
  public string name;
   
  public virtual void Test()
  {
  //虚方法可以写逻辑
  }
  public abstract void Bad();//抽象方法
  }
  class Apple : Fruits
  {
  //虚方法在子类中可以选择是否重写
  //抽象方法一定要重写
  public override void Bad()
  {
   
  }
  }

3 接口

3.1 基本概念

关键字:interface,接口是行为的抽象规范,是抽象行为的“基类”,各种类通过继承它来实现对应的行为。

接口声明规范:

  1. 不包含成员变量

  2. 只包含方法、属性、索引器、事件

  3. 成员不能被实现

  4. 成员可以不写访问修饰符,而且不能是私有的

  5. 接口不能继承类,但是接口可以继承另一个接口

接口使用规范:

  1. 类可以继承多个接口

  2. 类继承接口后,必须实现接口中所有成员

特点:

  1. 它和类的声明类似

  2. 接口是用来继承的

  3. 接口不能被实例化,但可以作为容器来存储对象

3.2 声明


 
  /*
  interface 接口名
  {
   
  }
  接口名:I+帕斯卡命名法
  */
  interface IFly
  {
  void Fly();//方法
   
  string Name//属性
  {
  get;
  set;
  }
   
  int this[int index]//索引
  {
  get;
  set;
  }
   
  event Action doSomething;//事件
  }

3.3 使用

接口用来继承:

  1. 类可以继承1个类,n个接口

  2. 继承了接口后,必须实现其中的内容,而且必须为 public 的

  3. 实现的接口函数,可以加 visual 再在子类中 override

  4. 接口遵循里氏替换原则


 
  class Animal
  {
   
  }
  class Tiger : Animal, IFly//继承类和接口
  {
  //实现接口内容
  public void Fly()
  {
   
  }
  public string Name
  {
  get;
  set;
  }
  public int this[int index]
  {
  get
  {
  return 0;
  }
  set
  {
   
  }
  }
  public event Action doSomething;
  }

3.4 接口继承接口

  • 接口继承接口时,不需要实现

  • 类继承接口后,去实现接口的所有内容


 
  interface IWalk
  {
  void Walk();
  }
   
  interface IMove : IFly, IWalk
  {
   
  }
   
  class Test : IMove//实现所有内容
  {
  public int this[int index]
  {
  get => throw new NotImplementedException(); set => throw new NotImplementedException();
  }
   
  public string Name
  {
  get => throw new NotImplementedException(); set => throw new NotImplementedException();
  }
   
  public event Action doSomething;
   
  public void Fly()
  {
  throw new NotImplementedException();
  }
   
  public void Walk()
  {
  throw new NotImplementedException();
  }
  }

3.5 显示实现接口

  • 当一个类继承两个接口,但是接口中存在着同名方法时使用

  • 注意:显示实现接口时,不能写访问修饰符


 
  interface IAtk
  {
  void Atk();
  }
  interface ISuperAtk
  {
  void Atk();
  }
   
  class Player : IAtk, ISuperAtk
  {
  //显示实现接口:接口名.行为名
  void IAtk.Atk()
  {
   
  }
  void ISuperAtk.Atk()
  {
   
  }
  }

4 sealed 密封方法

4.1 基本概念

  • 密封方法:用 sealed 修饰的重写函数,让虚方法或抽象方法不能再被重写,和 override 一起出现

  • 密封类:用 sealed 修饰的类,让类不能被继承

4.2 使用


 
  abstract class Animal
  {
  public string name;
   
  public abstract void Eat();
   
  public virtual void Speak()
  {
  Console.WriteLine("giao");
  }
  }
   
  class Person : Animal
  {
  public override void Eat()
  {
   
  }
   
  public override void Speak()
  {
   
  }
  }
   
  class WhitePerson : Person
  {
  public sealed override void Eat()//不能重写了
  {
  base.Eat();
  }
   
  public override void Speak()
  {
  base.Speak();
  }
  }