2024-11-15《继续c#学习-多态性》

多态性

静态多态性

函数重载

  函数重载就是一个函数可以通过传入不同的参数来进行不同的运算,例如:

  using System;
  namespace PolymorphismApplication
  {
  public class TestData
  {
  public int Add(int a, int b, int c)
  {
  return a + b + c;
  }
  public int Add(int a, int b)
  {
  return a + b;
  }
  }
  class Program
  {
  static void Main(string[] args)
  {
  TestData dataClass = new TestData();
  int add1 = dataClass.Add(1, 2);
  int add2 = dataClass.Add(1, 2, 3);
   
  Console.WriteLine("add1 :" + add1);
  Console.WriteLine("add2 :" + add2);
  }
  }
  }

运算符重载

  用法与函数重载相同:

  using System;
  namespace PolymorphismApplication
  {
  class Printdata
  {
  void print(int i)
  {
  Console.WriteLine("输出整型: {0}", i );
  }
   
  void print(double f)
  {
  Console.WriteLine("输出浮点型: {0}" , f);
  }
   
  void print(string s)
  {
  Console.WriteLine("输出字符串: {0}", s);
  }
  static void Main(string[] args)
  {
  Printdata p = new Printdata();
  // 调用 print 来打印整数
  p.print(1);
  // 调用 print 来打印浮点数
  p.print(1.23);
  // 调用 print 来打印字符串
  p.print("Hello Runoob");
  Console.ReadKey();
  }
  }
  }

动态多态性

  可以通过abstract关键字来创建一个抽象类,里面有抽象方法,可以被派生类实现。

  我们也可以用sealed关键字来实现密封,密封之后的类不能够被继承且无法使用抽象类。

  using System;
  namespace PolymorphismApplication
  {
  abstract class Shape
  {
  abstract public int area();
  }
  class Rectangle: Shape
  {
  private int length;
  private int width;
  public Rectangle( int a=0, int b=0)
  {
  length = a;
  width = b;
  }
  public override int area ()
  {
  Console.WriteLine("Rectangle 类的面积:");
  return (width * length);
  }
  }
   
  class RectangleTester
  {
  static void Main(string[] args)
  {
  Rectangle r = new Rectangle(10, 7);
  double a = r.area();
  Console.WriteLine("面积: {0}",a);
  Console.ReadKey();
  }
  }
  }

  如果我们想在继承类里实现基类的方法,可以用虚方法,关键字virtual来实现。

  动态多态性是通过抽象类虚方法实现的。

  using System;
  using System.Collections.Generic;
   
  public class Shape
  {
  public int X { get; private set; }
  public int Y { get; private set; }
  public int Height { get; set; }
  public int Width { get; set; }
   
  // 虚方法
  public virtual void Draw()
  {
  Console.WriteLine("执行基类的画图任务");
  }
  }
   
  class Circle : Shape
  {
  public override void Draw()
  {
  Console.WriteLine("画一个圆形");
  base.Draw();
  }
  }
  class Rectangle : Shape
  {
  public override void Draw()
  {
  Console.WriteLine("画一个长方形");
  base.Draw();
  }
  }
  class Triangle : Shape
  {
  public override void Draw()
  {
  Console.WriteLine("画一个三角形");
  base.Draw();
  }
  }
   
  class Program
  {
  static void Main(string[] args)
  {
  // 创建一个 List<Shape> 对象,并向该对象添加 Circle、Triangle 和 Rectangle
  var shapes = new List<Shape>
  {
  new Rectangle(),
  new Triangle(),
  new Circle()
  };
   
  // 使用 foreach 循环对该列表的派生类进行循环访问,并对其中的每个 Shape 对象调用 Draw 方法
  foreach (var shape in shapes)
  {
  shape.Draw();
  }
   
  Console.WriteLine("按下任意键退出。");
  Console.ReadKey();
  }
   
  }

运算符重载

  运算符重载在我看来就是让运算更加的方便省事,可以节省代码。

image-20221014160122665

  举个例子:

  using System;
   
  namespace OperatorOvlApplication
  {
  class Box
  {
  private double length; // 长度
  private double breadth; // 宽度
  private double height; // 高度
   
  public double getVolume()
  {
  return length * breadth * height;
  }
  public void setLength( double len )
  {
  length = len;
  }
   
  public void setBreadth( double bre )
  {
  breadth = bre;
  }
   
  public void setHeight( double hei )
  {
  height = hei;
  }
  // 重载 + 运算符来把两个 Box 对象相加
  public static Box operator+ (Box b, Box c)
  {
  Box box = new Box();
  box.length = b.length + c.length;
  box.breadth = b.breadth + c.breadth;
  box.height = b.height + c.height;
  return box;
  }
   
  public static bool operator == (Box lhs, Box rhs)
  {
  bool status = false;
  if (lhs.length == rhs.length && lhs.height == rhs.height
  && lhs.breadth == rhs.breadth)
  {
  status = true;
  }
  return status;
  }
  public static bool operator !=(Box lhs, Box rhs)
  {
  bool status = false;
  if (lhs.length != rhs.length || lhs.height != rhs.height
  || lhs.breadth != rhs.breadth)
  {
  status = true;
  }
  return status;
  }
  public static bool operator <(Box lhs, Box rhs)
  {
  bool status = false;
  if (lhs.length < rhs.length && lhs.height
  < rhs.height && lhs.breadth < rhs.breadth)
  {
  status = true;
  }
  return status;
  }
   
  public static bool operator >(Box lhs, Box rhs)
  {
  bool status = false;
  if (lhs.length > rhs.length && lhs.height
  > rhs.height && lhs.breadth > rhs.breadth)
  {
  status = true;
  }
  return status;
  }
   
  public static bool operator <=(Box lhs, Box rhs)
  {
  bool status = false;
  if (lhs.length <= rhs.length && lhs.height
  <= rhs.height && lhs.breadth <= rhs.breadth)
  {
  status = true;
  }
  return status;
  }
   
  public static bool operator >=(Box lhs, Box rhs)
  {
  bool status = false;
  if (lhs.length >= rhs.length && lhs.height
  >= rhs.height && lhs.breadth >= rhs.breadth)
  {
  status = true;
  }
  return status;
  }
  public override string ToString()
  {
  return String.Format("({0}, {1}, {2})", length, breadth, height);
  }
   
  }
   
  class Tester
  {
  static void Main(string[] args)
  {
  Box Box1 = new Box(); // 声明 Box1,类型为 Box
  Box Box2 = new Box(); // 声明 Box2,类型为 Box
  Box Box3 = new Box(); // 声明 Box3,类型为 Box
  Box Box4 = new Box();
  double volume = 0.0; // 体积
   
  // Box1 详述
  Box1.setLength(6.0);
  Box1.setBreadth(7.0);
  Box1.setHeight(5.0);
   
  // Box2 详述
  Box2.setLength(12.0);
  Box2.setBreadth(13.0);
  Box2.setHeight(10.0);
   
  // 使用重载的 ToString() 显示两个盒子
  Console.WriteLine("Box1: {0}", Box1.ToString());
  Console.WriteLine("Box2: {0}", Box2.ToString());
   
  // Box1 的体积
  volume = Box1.getVolume();
  Console.WriteLine("Box1 的体积: {0}", volume);
   
  // Box2 的体积
  volume = Box2.getVolume();
  Console.WriteLine("Box2 的体积: {0}", volume);
   
  // 把两个对象相加
  Box3 = Box1 + Box2;
  Console.WriteLine("Box3: {0}", Box3.ToString());
  // Box3 的体积
  volume = Box3.getVolume();
  Console.WriteLine("Box3 的体积: {0}", volume);
   
  //comparing the boxes
  if (Box1 > Box2)
  Console.WriteLine("Box1 大于 Box2");
  else
  Console.WriteLine("Box1 不大于 Box2");
  if (Box1 < Box2)
  Console.WriteLine("Box1 小于 Box2");
  else
  Console.WriteLine("Box1 不小于 Box2");
  if (Box1 >= Box2)
  Console.WriteLine("Box1 大于等于 Box2");
  else
  Console.WriteLine("Box1 不大于等于 Box2");
  if (Box1 <= Box2)
  Console.WriteLine("Box1 小于等于 Box2");
  else
  Console.WriteLine("Box1 不小于等于 Box2");
  if (Box1 != Box2)
  Console.WriteLine("Box1 不等于 Box2");
  else
  Console.WriteLine("Box1 等于 Box2");
  Box4 = Box3;
  if (Box3 == Box4)
  Console.WriteLine("Box3 等于 Box4");
  else
  Console.WriteLine("Box3 不等于 Box4");
   
  Console.ReadKey();
  }
  }
  }
posted @   new菜鸟  阅读(4)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)
点击右上角即可分享
微信分享提示