ToB企服应用市场:ToB评测及商务社交产业平台

标题: C#面向对象核心-多态 [打印本页]

作者: 民工心事    时间: 2023-4-4 14:25
标题: C#面向对象核心-多态
多态

1 认识多态

1.1 基本概念

多态是同一个行为具有多个不同表现形式或形态的能力,意味着有多重形式。在面向对象编程范式中,多态性往往表现为"一个接口,多个功能"。
在 C# 中,每个类型都是多态的,因为包括用户定义类型在内的所有类型都继承自 Object。
多态性分为静态的和动态多态。在静态多态性中,函数的响应是在编译时发生的。在动态多态性中,函数的响应是在运行时发生的。
1.2 使用
  1. class GameObject
  2. {
  3.     public string name;
  4.     public GameObject(string name)
  5.     {
  6.         this.name = name;
  7.     }
  8.     //虚函数 可以被子类重写
  9.     public virtual void Atk()
  10.     {
  11.         Console.WriteLine("游戏对象进行攻击");
  12.     }
  13. }
  14. class Player : GameObject
  15. {
  16.     public Player(string name) : base(name)//调用父类的构造函数
  17.     {
  18.     }
  19.     //重写虚函数
  20.     public override void Atk()
  21.     {
  22.         Console.WriteLine("玩家对象进行攻击");
  23.     }
  24. }
  25. class Monster : GameObject
  26. {
  27.     public Monster(string name) : base(name)
  28.     {
  29.     }
  30.     public override void Atk()
  31.     {
  32.         base.Atk();//base代表父类,可以通过它来保留父类的行为,会调用一次父类的方法
  33.         Console.WriteLine("怪物对象进行攻击");
  34.     }
  35. }
  36. class Father
  37. {
  38.     public void SpeakName()
  39.     {
  40.         Console.WriteLine("Father的方法");
  41.     }
  42. }
  43. class Son : Father
  44. {
  45.     public new void SpeakName()
  46.     {
  47.         Console.WriteLine("Son的方法");
  48.     }
  49. }
  50. //Main
  51. //用父类取装载子类的对象时,有两个同名的方法,会优先调用父类的 多态就用来解决这类问题
  52. Father f = new Son();
  53. f.SpeakName();
  54. (f as Son).SpeakName();
  55. GameObject p = new Player("abc");//虚函数和重写解决了问题
  56. p.Atk();
  57. GameObject m = new Monster("def");
  58. m.Atk();
  59. /*
  60. 输出:
  61. Father的方法
  62. Son的方法
  63. 玩家对象进行攻击
  64. 游戏对象进行攻击
  65. 怪物对象进行攻击
  66. */
复制代码
2 abstract 抽象类和抽象方法

2.1 抽象类

abstract 关键字修饰的类。
特点:
  1. abstract class Thing//抽象一类物品
  2. {
  3.     public string name;
  4.    
  5.     //可以在抽象类中写抽象函数
  6. }
  7. class Water : Thing
  8. {
  9. }
复制代码
2.2 抽象方法

abstract 修饰的方法,又叫 纯虚方法
特点:
  1. abstract class Fruits
  2. {
  3.     public string name;
  4.     public virtual void Test()
  5.     {
  6.         //虚方法可以写逻辑
  7.     }
  8.     public abstract void Bad();//抽象方法
  9. }
  10. class Apple : Fruits
  11. {
  12.     //虚方法在子类中可以选择是否重写
  13.     //抽象方法一定要重写
  14.     public override void Bad()
  15.     {
  16.     }
  17. }
复制代码
3 接口

3.1 基本概念

关键字:interface,接口是行为的抽象规范,是抽象行为的“基类”,各种类通过继承它来实现对应的行为。
接口声明规范:
接口使用规范:
特点:
3.2 声明
  1. /*
  2. interface 接口名
  3. {
  4. }
  5. 接口名:I+帕斯卡命名法
  6. */
  7. interface IFly
  8. {
  9.     void Fly();//方法
  10.     string Name//属性
  11.     {
  12.         get;
  13.         set;
  14.     }
  15.     int this[int index]//索引
  16.     {
  17.         get;
  18.         set;
  19.     }
  20.     event Action doSomething;//事件
  21. }
复制代码
3.3 使用

接口用来继承:
  1. class Animal
  2. {
  3. }
  4. class Tiger : Animal, IFly//继承类和接口
  5. {
  6.     //实现接口内容
  7.     public void Fly()
  8.     {
  9.     }
  10.     public string Name
  11.     {
  12.         get;
  13.         set;
  14.     }
  15.     public int this[int index]
  16.     {
  17.         get
  18.         {
  19.             return 0;
  20.         }
  21.         set
  22.         {
  23.         }
  24.     }
  25.     public event Action doSomething;
  26. }
复制代码
3.4 接口继承接口

  1. interface IWalk
  2. {
  3.     void Walk();
  4. }
  5. interface IMove : IFly, IWalk
  6. {
  7. }
  8. class Test : IMove//实现所有内容
  9. {
  10.     public int this[int index]
  11.     {
  12.         get => throw new NotImplementedException(); set => throw new NotImplementedException();
  13.     }
  14.     public string Name
  15.     {
  16.         get => throw new NotImplementedException(); set => throw new NotImplementedException();
  17.     }
  18.     public event Action doSomething;
  19.     public void Fly()
  20.     {
  21.         throw new NotImplementedException();
  22.     }
  23.     public void Walk()
  24.     {
  25.         throw new NotImplementedException();
  26.     }
  27. }
复制代码
3.5 显示实现接口

  1. interface IAtk
  2. {
  3.     void Atk();
  4. }
  5. interface ISuperAtk
  6. {
  7.     void Atk();
  8. }
  9. class Player : IAtk, ISuperAtk
  10. {
  11.     //显示实现接口:接口名.行为名
  12.     void IAtk.Atk()
  13.     {
  14.     }
  15.     void ISuperAtk.Atk()
  16.     {
  17.     }
  18. }
复制代码
4 sealed 密封方法

4.1 基本概念

4.2 使用
  1. abstract class Animal
  2. {
  3.     public string name;
  4.     public abstract void Eat();
  5.     public virtual void Speak()
  6.     {
  7.         Console.WriteLine("giao");
  8.     }
  9. }
  10. class Person : Animal
  11. {
  12.     public override void Eat()
  13.     {
  14.     }
  15.     public override void Speak()
  16.     {
  17.     }
  18. }
  19. class WhitePerson : Person
  20. {
  21.     public sealed override void Eat()//不能重写了
  22.     {
  23.         base.Eat();
  24.     }
  25.     public override void Speak()
  26.     {
  27.         base.Speak();
  28.     }
  29. }
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!




欢迎光临 ToB企服应用市场:ToB评测及商务社交产业平台 (https://dis.qidao123.com/) Powered by Discuz! X3.4