20.枚举(Enum)
枚举是一组命名整型常量。枚举类型是使用 enum 关键字声明的。
C# 枚举是值类型。换句话说,枚举包含自己的值,且不能继承或传递继承。
声明枚举语法:enum <enum_name> {enumeration list}; //enum_name为枚举名称,enumeration list是一个用逗号分隔的标识符列表
枚举列表中的每个符号代表一个整数值,一个比它前面的符号大的整数值。默认情况下,第一个枚举符号的值是 0
例如:enum Days { Sun, Mon, tue, Wed, thu, Fri, Sat };
实例:
public class EnumTest
{
enum Days { Sun, Mon, tue, Wed, thu, Fri, Sat };
static void Main(String[]args)
{
int x = (int)Day.Sun;
int y = (int)Day.Fri;
Console.WriteLine("Sun = {0}", x); // 0
Console.WriteLine("Fri = {0}", y); // 5
}
}
扩展笔记:
1.枚举列表中的每个符号代表一个整数值,一个比它前面的符号大的整数值。默认情况下,第一个枚举符号的值是 0。但是,你也可以自定义每个符号的值:
enum Days { Mon=71, tue=61, Wed==51, thu=41, Fri=51, Sat=61,Sun=71 };
int WeekdayStart = (int)Days.Mon;
int WeekdayEnd = (int)Days.Fri;
bool b = WeekdayStart == Sun;
Console.WriteLine("WeekdayStart == Sun:{0}",b);// true
Console.WriteLine("Monday: {0}", WeekdayStart); //71
Console.WriteLine("Friday: {0}", WeekdayEnd); //51
2.枚举列表中的每个符号代表一个整数值,一个比它前面的符号大的整数值。默认情况下,第一个枚举符号的值是 0。第 n 个符号值与第 n-1 个有关。
enum Day {a=8,b,c=1,e,f,g};
int a = (int)Day.a;
int b = (int)Day.b;
int e = (int)Day.e;
int f = (int)Day.f;
int g = (int)Day.g;
Console.WriteLine(a); //8
Console.WriteLine(b); //9
Console.WriteLine(e); //2
Console.WriteLine(f); //3
Console.WriteLine(g); //4
Console.ReadKey();
3.枚举体在给数组中具体标号位置给予含义,可以使程序更容易读懂和修改。
enum len { Length, width, height};
static void Main(string[] args)
{
int[] parameter = new int[3] {1,5,8};
Console.WriteLine("Length: {0}", parameter[(int)len.Length]);
Console.WriteLine("width: {0}", parameter[(int)len.width]);
Console.WriteLine("height: {0}", parameter[(int)len.height]);
}
4.枚举类名建议带上 Enum 后缀,枚举成员名称需要全大写,单词间用下划线隔开。 枚举其实就是特殊的常量类,且构造方法被默认强制是私有。
正例:枚举名字: DealStatusEnum, 成员名称: SUCCESS / UNKOWN_REASON
5.枚举是一种类型,适用于某些取值范围有限的数据,枚举访问权限修饰符和类是一样的,默认访问权限都是internal,枚举名遵循大驼峰命名法,枚举的每一个值都是整形,默认都从0开始
枚举中元素的值可以设置为相同,但不推荐。枚举元素默认的值的类型都是int类型,可以修改为其他整形byte、sbyte、short、ushort、int、uint、long、ulong
6.枚举可以直接在类中声明引用,也可以在类外使用
enum转int:enum Day {a=8,b,c=1,e,f,g};int a = (int)Day.a;
int转enum:int number = 10;Console.WriteLine((enum)number);
21.类(Class)
定义一个类时,定义了一个数据类型的蓝图。这实际上并没有定义任何的数据,但它定义了类的名称意味着什么,也就是说,类的对象由什么组成及在这个对象上可执行什么操作。
对象是类的实例。构成类的方法和变量称为类的成员。
类的定义:以关键字 class 开始,后跟类的名称。类的主体,包含在一对花括号内。下面是类定义的一般形式
访问修饰符 class 类名称 {//变量 修饰符 类型 变量名; //方法 修饰符 返回值类型 方法名(参数){//方法体}}
类的默认访问标识符是 internal,成员的访问标识符是 private
如果要访问类的成员,你要使用点(.)运算符,点运算符链接了对象的名称和成员的名称
实例:
using System;
namespace BoxApplication
{
class Box
{
public double length; // 长度
public double breadth; // 宽度
public double height; // 高度
}
class Boxtester
{
static void Main(string[] args)
{
Box Box1 = new Box(); // 声明 Box1,类型为 Box
Box Box2 = new Box(); // 声明 Box2,类型为 Box
double volume = 0.0; // 体积
// Box1 详述
Box1.height = 5.0;
Box1.length = 6.0;
Box1.breadth = 7.0;
// Box2 详述
Box2.height = 10.0;
Box2.length = 12.0;
Box2.breadth = 13.0;
// Box1 的体积
volume = Box1.height * Box1.length * Box1.breadth;
Console.WriteLine("Box1 的体积: {0}", volume); //210
// Box2 的体积
volume = Box2.height * Box2.length * Box2.breadth;
Console.WriteLine("Box2 的体积: {0}", volume);//1560
Console.ReadKey();
}
}
}
成员函数和封装:使用private对属性进行封装私有化,然后使用方法去设置属性值
类的成员函数是一个在类定义中有它的定义或原型的函数,就像其他变量一样。作为类的一个成员,它能在类的任何对象上操作,且能访问该对象的类的所有成员。
成员变量是对象的属性(从设计角度),且它们保持私有来实现封装。这些变量只能使用公共成员函数来访问。
实例:
using System;
namespace BoxApplication
{
class Box
{
private double length; // 长度
private double breadth; // 宽度
private double height; // 高度
public void setLength( double len )
{
length = len;
}
public void setBreadth( double bre )
{
breadth = bre;
}
public void setHeight( double hei )
{
height = hei;
}
public double getVolume()
{
return length * breadth * height;
}
}
class Boxtester
{
static void Main(string[] args)
{
Box Box1 = new Box(); // 声明 Box1,类型为 Box
Box Box2 = new Box(); // 声明 Box2,类型为 Box
double volume; // 体积
// 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);
// Box1 的体积
volume = Box1.getVolume();
Console.WriteLine("Box1 的体积: {0}" ,volume);//210
// Box2 的体积
volume = Box2.getVolume();
Console.WriteLine("Box2 的体积: {0}", volume);//1560
Console.ReadKey();
}
}
}
构造函数:类的 构造函数 是类的一个特殊的成员函数,当创建类的新对象时执行。构造函数的名称与类的名称完全相同,它没有任何返回类型。
实例:
class Line
{
public Line(){//方法体}
}
默认的构造函数没有任何参数。但是如果你需要一个带有参数的构造函数可以有参数,这种构造函数叫做参数化构造函数。
实例:
class Line
{
private double length;
public Line(double len)
{
length = len;
}
}
析构函数:类的 析构函数 是类的一个特殊的成员函数,当类的对象超出范围时执行
析构函数的名称是在类的名称前加上一个波浪形(~)作为前缀,它不返回值,也不带任何参数。
析构函数用于在结束程序(比如关闭文件、释放内存等)之前释放资源。析构函数不能继承或重载。
实例:
class Line
{
public Line(){Console.WriteLine("对象已创建");}
public void setLength( double len )
{
length = len;
}
~Line(){Console.WriteLine("对象已删除");}
static void Main(string[] args)
{
Line line = new Line();
// 设置线条长度
line.setLength(6.0);
Console.WriteLine("线条的长度: {0}", line.getLength());
}
//输出结果:
//对象已创建
//线条的长度: 6
//对象已删除
}
静态成员:
可以使用 static 关键字把类成员定义为静态的。
当我们声明一个类成员为静态时,意味着无论有多少个类的对象被创建,只会有一个该静态成员的副本。
静态变量用于定义常量,因为它们的值可以通过直接调用类而不需要创建类的实例来获取。
静态变量可在成员函数或类的定义外部进行初始化。关键字static意味着类中只有一个该成员的实例。你也可以在类的定义内部初始化静态变量。
实例-静态变量:
using System;
namespace StaticVarApplication
{
class StaticVar
{
public static int num;
public void count()
{
num++;
}
public int getNum()
{
return num;
}
}
class StaticTester
{
static void Main(string[] args)
{
StaticVar s1 = new StaticVar();
StaticVar s2 = new StaticVar();
s1.count();
s1.count();
s1.count();
s2.count();
s2.count();
s2.count();
Console.WriteLine("s1 的变量 num: {0}", s1.getNum());//6
Console.WriteLine("s2 的变量 num: {0}", s2.getNum());//6
Console.ReadKey();
}
}
}
实例-静态函数:static修饰声明成员函数,静态函数只能访问静态静态变量,静态函数在对象被创建之前就存在。
using System;
namespace StaticVarApplication
{
class StaticVar
{
public static int num;
public void count()
{
num++;
}
public static int getNum()
{
return num;
}
}
class StaticTester
{
static void Main(string[] args)
{
StaticVar s = new StaticVar();
s.count();
s.count();
s.count();
Console.WriteLine("变量 num: {0}", StaticVar.getNum());
Console.ReadKey();
}
}
}
扩展笔记:
1.将类成员函数声明为public static无需实例化即可调用类成员函数,反之,如果不声明为static,即使和Main方法从属于同一个类,也必须经过实例化
2.类中没有显示提供实例构造函数,编译器会默认提供一个无参构造函数,一旦声明了构造函数,默认无参构造函数则不会声明
22.继承
继承是面向对象程序设计中最重要的概念之一。继承允许我们根据一个类来定义另一个类,这使得创建和维护应用程序变得更容易。同时也有利于重用代码和节省开发时间。
当创建一个类时,程序员不需要完全重新编写新的数据成员和成员函数,只需要设计一个新的类,继承了已有的类的成员即可。这个已有的类被称为的基类,这个新的类被称为派生类。
继承的思想实现了 属于(IS-A) 关系。
基类和派生类:
一个类可以继承自另一个类,被称为基类(父类)和派生类(子类)。
C# 不支持类的多重继承,但支持接口的多重继承,一个类可以实现多个接口。
概括来说:一个类可以继承多个接口,但只能继承自一个类。
创建派生类语法:
访问修饰符 class 基类名{...}
class 派生类名 : 基类名{...}
派生类会继承基类的成员(字段、方法、属性等),除非它们被明确地标记为私有(private)。
派生类可以通过关键字base来调用基类的构造函数和方法。
实例:
class BaseClass
{
public void SomeMethod()
{
// Method implementation
}
}
class DerivedClass : BaseClass
{
public void AnotherMethod()
{
// Accessing base class method
base.SomeMethod();
// Method implementation
}
}
基类的初始化:派生类继承了基类的成员变量和成员方法。因此父类对象应在子类对象创建之前被创建。您可以在成员初始化列表中使用base进行父类的初始化。
实例:
using System;
namespace RectangleApplication
{
class Rectangle
{
// 成员变量
protected double length;
protected double width;
public Rectangle(double l, double w)
{
length = l;
width = w;
}
public double GetArea()
{
return length * width;
}
public void Display()
{
Console.WriteLine("长度: {0}", length);
Console.WriteLine("宽度: {0}", width);
Console.WriteLine("面积: {0}", GetArea());
}
}
class Tabletop : Rectangle
{
private double cost;
public Tabletop(double l, double w) : base(l, w)
{ }
public double GetCost()
{
double cost;
cost = GetArea() * 70;
return cost;
}
public void Display()
{
base.Display();
Console.WriteLine("成本: {0}", GetCost());
}
}
class ExecuteRectangle
{
static void Main(string[] args)
{
Tabletop t = new Tabletop(4.5, 7.5);
t.Display();
Console.ReadLine();
//输出结果:
长度: 4.5
宽度: 7.5
面积: 33.75
成本: 2362.5
}
}
}
继承接口:
一个接口可以继承自一个或多个其他接口,派生接口继承了基接口的所有成员。
派生接口可以扩展基接口的成员列表,但不能改变它们的访问修饰符。
实例1:
interface IBaseInterface
{
void Method1();
}
interface IDerivedInterface : IBaseInterface
{
void Method2();
}
实例2:
using System;
// 定义一个基接口
interface IBaseInterface
{
void Method1();
}
// 定义一个派生接口,继承自基接口
interface IDerivedInterface : IBaseInterface
{
void Method2();
}
// 实现派生接口的类
class MyClass : IDerivedInterface
{
public void Method1()
{
Console.WriteLine("Method1 implementation");
}
public void Method2()
{
Console.WriteLine("Method2 implementation");
}
}
class Program
{
static void Main(string[] args)
{
// 创建 MyClass 类的实例
MyClass obj = new MyClass();
// 调用继承自基接口的方法
obj.Method1();
// 调用派生接口新增的方法
obj.Method2();
}
}
多重继承:多重继承指的是一个类别可以同时从多于一个父类继承行为与特征的功能。与单一继承相对,单一继承指一个类别只可以继承自一个父类。
C# 不支持多重继承。但是,您可以使用接口来实现多重继承。
实例:
using System;
namespace InheritanceApplication
{
class Shape
{
public void setWidth(int w)
{
width = w;
}
public void setHeight(int h)
{
height = h;
}
protected int width;
protected int height;
}
// 基类 PaintCost
public interface PaintCost
{
int getCost(int area);
}
// 派生类
class Rectangle : Shape, PaintCost
{
public int getArea()
{
return (width * height);
}
public int getCost(int area)
{
return area * 70;
}
}
class RectangleTester
{
static void Main(string[] args)
{
Rectangle Rect = new Rectangle();
int area;
Rect.setWidth(5);
Rect.setHeight(7);
area = Rect.getArea();
// 打印对象的面积
Console.WriteLine("总面积: {0}", Rect.getArea());//35
Console.WriteLine("油漆总成本: ${0}" , Rect.getCost(area));//2450
Console.ReadKey();
}
}
}
23.多态性
多态是同一个行为具有多个不同表现形式或形态的能力。
多态性意味着有多重形式。在面向对象编程范式中,多态性往往表现为"一个接口,多个功能"。
多态性可以是静态的或动态的。在静态多态性中,函数的响应是在编译时发生的。在动态多态性中,函数的响应是在运行时发生的。
在 C# 中,每个类型都是多态的,因为包括用户定义类型在内的所有类型都继承自 Object。
多态就是同一个接口,使用不同的实例而执行不同操作.
静态多态性:在编译时,函数和对象的连接机制被称为早期绑定,也被称为静态绑定。C# 提供了两种技术来实现静态多态性,分别为函数重载和运算符重载。
函数重载:在编译时,函数和对象的连接机制被称为早期绑定,也被称为静态绑定。C# 提供了两种技术来实现静态多态性。
实例:类中有多个Add方法,但是参数不同
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);
}
}
}
动态多态性:
使用关键字 abstract 创建抽象类,用于提供接口的部分类的实现。
当一个派生类继承自该抽象类时,实现即完成。抽象类包含抽象方法,抽象方法可被派生类实现。派生类具有更专业的功能。
抽象类规则:不能创建抽象类实例,不能在抽象类外部声明抽象方法。
通过在类定义前面放置关键字 sealed,可以将类声明为密封类。当一个类被声明为 sealed 时,它不能被继承。抽象类不能被声明为 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 声明的。
虚方法可以在不同的继承类中有不同的实现。
对虚方法的调用是在运行时发生的。
动态多态性是通过 抽象类 和 虚方法 实现的。
实例1:创建Shape基类,并创建派生类Cirsle,Rectangle,Triangle.Shape提供了一个Draw的虚拟方法,派生类中重写基类的虚拟方法分别绘制该类的指定形状
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();
//输出结果:
画一个长方形
执行基类的画图任务
画一个三角形
执行基类的画图任务
画一个圆形
执行基类的画图任务
按下任意键退出。
}
}
实例2:通过虚拟方法area计算不同形状的面积
using System;
namespace PolymorphismApplication
{
class Shape
{
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 ()
{
Console.WriteLine("Rectangle 类的面积:");
return (width * height);
}
}
class Triangle: Shape
{
public Triangle(int a = 0, int b = 0): base(a, b)
{
}
public override int area()
{
Console.WriteLine("Triangle 类的面积:");
return (width * height / 2);
}
}
class Caller
{
public void CallArea(Shape sh)
{
int a;
a = sh.area();
Console.WriteLine("面积: {0}", a);
}
}
class Tester
{
static void Main(string[] args)
{
Caller c = new Caller();
Rectangle r = new Rectangle(10, 7);
Triangle t = new Triangle(10, 5);
c.CallArea(r);
c.CallArea(t);
Console.ReadKey();
//输出结果:
Rectangle 类的面积:
面积:70
Triangle 类的面积:
面积:25
}
}
}
24.运算符重载
可以重定义或重载 C# 中内置的运算符。可以使用用户自定义类型的运算符。
重载运算符是具有特殊名称的函数,是通过关键字 operator 后跟运算符的符号来定义的。与其他函数一样,重载运算符有返回类型和参数列表。
实例:为用户自定义的类 Box 实现了加法运算符(+)。它把两个 Box 对象的属性相加,并返回相加后的 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 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;
}
Box box1 = new Box(6,7,5);
Box box2 = new Box(12,13,10);
Box box3 = box1+box2;
//最终box3的体积为 18*13*15=5400
可重载和不可重载运算符:
可重载一元运算符:+, -, !, ~, ++, --
可重载二元运算符:+, -, *, /, %
可重载比较运算符:==, !=, <, >, <=, >=
不可重载条件逻辑运算符:&&、||
不可重载复制运算符:+=, -=, *=, /=, %=
不可重载运算符:=, ., ?:, ->, new, is, sizeof, typeof
实例: 以下方法从上至下分别依次重载==、!=、<、>、<=、>=
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;
}
25.接口(Interface)
接口定义了所有类继承接口时应遵循的语法合同。接口定义了语法合同 "是什么" 部分,派生类定义了语法合同 "怎么做" 部分。
接口定义了属性、方法和事件,这些都是接口的成员。接口只包含了成员的声明。成员的定义是派生类的责任。接口提供了派生类应遵循的标准结构。
接口使得实现接口的类或结构在形式上保持一致。
接口本身并不实现任何功能,它只是和声明实现该接口的对象订立一个必须实现哪些行为的契约。
抽象类在某种程度上与接口类似,但是,它们大多只是用在当只有少数方法由基类声明由派生类实现时。
抽象类不能直接实例化,但允许派生出具体的,具有实际功能的类。
定义接口:使用关键字Interface声明,接口默认是public的,语法如下:
interface 接口名称{//void method();}
实现接口语法:class 类名称:接口名称{}
继承接口,需要实现接口的方法,方法名必须和接口定义的方法一致
接口之间的继承:interface 派生接口:基接口{}
如果一个接口继承了一个接口,有一个类实现了派生接口,那么实现类或结构就需要实现所有接口的成员。