# 静态多态性

---

## 1 函数重载

和C++一样。

---

## 2 运算符重载

public

static

operator

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;
}

 

不是所有的运算符都可以被重载。

+, -, !, ~, ++, -- 这些一元运算符只有一个操作数,且可以被重载。
+, -, *, /, % 这些二元运算符带有两个操作数,且可以被重载。
==, !=, <, >, <=, >= 这些比较运算符可以被重载。
&&, || 这些条件逻辑运算符不能被直接重载。
+=, -=, *=, /=, %= 这些赋值运算符不能被重载。
=, ., ?:, ->, new, is, sizeof, typeof 这些运算符不能被重载。

运算符只能采用值参数,不能采用 ref 或 out 参数。

C# 要求成对重载比较运算符。如果重载了==,则也必须重载!=,否则产生编译错误。同时,比较运算符必须返回bool类型的值,这是与其他算术运算符的根本区别。

C# 不允许重载=运算符,但如果重载例如+运算符,编译器会自动使用+运算符的重载来执行+=运算符的操作。

---

# 动态多态性

---

## 3 派生/继承

abstract 抽象类,父类、基类;

* 抽象类和普通类都可以被继承,但是抽象类不能实例化;

sealed 封闭类,不能被继承;

virtual 虚方法,虚函数;

override 子类重载虚函数的时候,需要这个关键字。

   abstract class Shape // abstract 可以没有,abstract表示不能实例化
{
protected int width, height;
public Shape( int a=0, int b=0)
{
width = a;
height = b;
}
public virtual int area() // 虚函数
{
Console.WriteLine("父类的面积:");
return 0;
}
}
class Rectangle: Shape
{
public Rectangle( int a=0, int b=0): base(a, b)
{ }
public override int area () // 重载虚函数 override
{
Console.WriteLine("Rectangle 类的面积:");
return (width * height);
}
}

  

---

参考:

http://www.runoob.com/csharp/csharp-operator-overloading.html

http://www.runoob.com/csharp/csharp-polymorphism.html

https://blog.csdn.net/t_twory/article/details/51543247

最新文章

  1. ConcurrentAsyncQueue 2014-09-07
  2. [field:picname/]和[field:litpic/]区别
  3. SQL Server索引调优系列
  4. The launch will only sync the application package on the device!
  5. UVA Knight Moves
  6. Mahout-Pearson correlation的实现
  7. MVC中的Startup.Auth.cs、BundleConfig.cs、FilterConfig.cs和RouteConfig.cs
  8. win10环境下Android SDK下载安装及配置教程
  9. bootstrap栅格系统中同行div高度不一致的解决方法
  10. POJ 2970 The lazy programmer
  11. i.e., e.g., etc.
  12. jenkins windows slave 报错ERROR: Error cloning remote repo &#39;origin&#39;
  13. Swagger2
  14. STL容器(C11)--unordered_map用法
  15. 分步理解 Promise 的实现
  16. [LOJ#6044]. 「雅礼集训 2017 Day8」共[二分图、prufer序列]
  17. 在oaf中集成SpringLoaded实现热部署
  18. UPDATE语句总结
  19. BZOJ1880: [Sdoi2009]Elaxia的路线(最短路)
  20. PageRank学习

热门文章

  1. [LeetCode] 55. Jump Game 跳跃游戏
  2. VUE引入jq bootstrap 之终极解决方案(测试)
  3. 使用JDBC连接MySQL数据库操作增删改查
  4. What IS MPI
  5. RabbitMQ实例C#
  6. swagger 集成后发布到服务器报错[Could not find file &#39;D:\\home\\site\\wwwroot\\bin\\WebAPI.XML]
  7. 纯C语言实现线性链表
  8. Redis之缓存雪崩、缓存穿透、缓存预热、缓存更新、缓存降级
  9. Robotframework ride ,运行后提示, [WinError 2] 系统找不到指定的文件。
  10. Java 之 Set 源码分析