C#学习笔记3

xiaoxiao2025-04-19  11

C#多态性

静态多态性:

函数重载 C#函数重载与Java类似,可以是参数列表中的参数类型不同,也可以是参数个数不同。

运算符重载: 可以重新定义或重载C#中内置的运算符,也可以使用用户自定义类型的运算符。重载运算符是具有特殊名称的函数,是通过关键字operator后跟运算符的符号来定义的。重载运算符有返回类型和参数列表。 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace OperatorOverloading { class Opreator { private int length; public int Length { get { return length; } set { length = value; } } private int breadth; public int Breadth { get { return breadth; } set { breadth = value; } } private int height; public int Height { get { return height; } set { height = value; } } public int getVolume() { return length * breadth * height; } public static Opreator operator+ (Opreator o1, Opreator o2) { Opreator o3=new Opreator(); o3.Height = o1.height + o2.height; o3.Length = o1.length + o2.length; o3.Breadth = o1.breadth + o2.breadth; return o3; } static void Main(string[] args) { Opreator o1 = new Opreator(); Opreator o2 = new Opreator(); Opreator o3 = new Opreator(); o1.Breadth = 3; o1.Height = 3; o1.Length = 3; o2.Length = 2; o2.Height = 2; o2.Breadth = 2; o3 = o2 + o1; int vol = o3.getVolume(); Console.WriteLine("{0}",vol); Console.ReadKey(); } } } 运算符重载

动态多态性:

抽象类 C#允许使用abstract创建抽象类,用于提供接口的部分类的实现。当一个派生类继承自该抽象类时,实现即完成。抽象类包含抽象方法,抽象方法可被派生类实现。派生类具有更专业的功能。

1. 抽象类不能创建实例 2. 不能在抽象类外部声明一个抽象方法 3. 通过在类定义前面放置关键字sealed,可以将类声明为密封类。当一个类被声明为sealed时,它不能被继承。抽象类不能被声明为sealed。

虚方法: 当有一个定义在类中的函数需要在继承类中实现时,可以使用虚方法。虚方法使用关键字virtual声明。虚方法可以在不同的继承类中有不同的实现。对虚方法的调用是在运行时发生的。

C#接口:

C#接口与Java类似,使用interface关键字声明,接口声明默认是public的。

C#预处理器指令:

预处理器指令指导编译器在实际编译开始之前对信息进行预处理 所有的预处理器指令都是以#开始,且在一行上,只有空白字符可以出现在预处理器指令之前。预处理器指令不是语句,所以不以分号结束。 C#编译器没有一个单独的预处理器,但是,指令被处理时就像是有一个单独的预处理器一样。在C#中,预处理器指令用于在条件编译中起作用。与C和C++不同,他们不是用来创建宏。一个预处理器指令必须是该行上的唯一指令。

C# I/O类:

FileStream类: Systom.IO命名空间中的FileStream类有助于文件的读写与关闭.该类派生自抽象类Stream。

转载请注明原文地址: https://www.6miu.com/read-5028624.html

最新回复(0)