- C# 基础教程
- C# - 主页
- C# - 概述
- C# - 环境
- C# - 程序结构
- C# - 基本语法
- C# - 数据类型
- C# - 类型转换
- C# - 变量
- C# - 常量
- C# - 运算符
- C# - 决策
- C# - 循环
- C# - 封装
- C# - 方法
- C# - 可空值
- C# - 数组
- C# - 字符串
- C# - 结构
- C# - 枚举
- C# - 类
- C# - 继承
- C# - 多态性
- C# - 运算符重载
- C# - 接口
- C# - 命名空间
- C# - 预处理器指令
- C# - 正则表达式
- C# - 异常处理
- C# - 文件 I/O
- C# 高级教程
- C# - 属性
- C# - 反射
- C# - 属性
- C# - 索引器
- C# - 委托
- C# - 事件
- C# - 集合
- C# - 泛型
- C# - 匿名方法
- C# - 不安全代码
- C# - 多线程
- C# 有用资源
- C# - 问题与解答
- C# - 快速指南
- C# - 有用的资源
- C# - 讨论
C# - 类
当您定义类时,您就定义了数据类型的蓝图。这实际上并没有定义任何数据,但它确实定义了类名的含义。也就是说,类的对象由什么组成以及可以对该对象执行哪些操作。对象是类的实例。构成类的方法和变量称为类的成员。
定义一个类
类定义以关键字 class 开头,后跟类名;以及由一对花括号括起来的类主体。以下是类定义的一般形式 -
<access specifier> class class_name {
// member variables
<access specifier> <data type> variable1;
<access specifier> <data type> variable2;
...
<access specifier> <data type> variableN;
// member methods
<access specifier> <return type> method1(parameter_list) {
// method body
}
<access specifier> <return type> method2(parameter_list) {
// method body
}
...
<access specifier> <return type> methodN(parameter_list) {
// method body
}
}
注意 -
访问说明符指定成员以及类本身的访问规则。如果没有提及,则类类型的默认访问说明符是内部。成员的默认访问权限是private。
数据类型指定变量的类型,返回类型指定方法返回的数据的数据类型(如果有)。
要访问类成员,可以使用点 (.) 运算符。
点运算符将对象的名称与成员的名称链接起来。
以下示例说明了迄今为止讨论的概念 -
using System;
namespace BoxApplication {
class Box {
public double length; // Length of a box
public double breadth; // Breadth of a box
public double height; // Height of a box
}
class Boxtester {
static void Main(string[] args) {
Box Box1 = new Box(); // Declare Box1 of type Box
Box Box2 = new Box(); // Declare Box2 of type Box
double volume = 0.0; // Store the volume of a box here
// box 1 specification
Box1.height = 5.0;
Box1.length = 6.0;
Box1.breadth = 7.0;
// box 2 specification
Box2.height = 10.0;
Box2.length = 12.0;
Box2.breadth = 13.0;
// volume of box 1
volume = Box1.height * Box1.length * Box1.breadth;
Console.WriteLine("Volume of Box1 : {0}", volume);
// volume of box 2
volume = Box2.height * Box2.length * Box2.breadth;
Console.WriteLine("Volume of Box2 : {0}", volume);
Console.ReadKey();
}
}
}
当上面的代码被编译并执行时,它会产生以下结果 -
Volume of Box1 : 210 Volume of Box2 : 1560
成员函数及封装
类的成员函数是在类定义中具有与任何其他变量类似的定义或原型的函数。它对它所属的类的任何对象进行操作,并且可以访问该对象的类的所有成员。
成员变量是对象的属性(从设计角度来看),并且它们保持私有以实现封装。这些变量只能使用公共成员函数来访问。
让我们将上述概念来设置和获取类中不同类成员的值 -
using System;
namespace BoxApplication {
class Box {
private double length; // Length of a box
private double breadth; // Breadth of a box
private double height; // Height of a box
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(); // Declare Box1 of type Box
Box Box2 = new Box();
double volume;
// Declare Box2 of type Box
// box 1 specification
Box1.setLength(6.0);
Box1.setBreadth(7.0);
Box1.setHeight(5.0);
// box 2 specification
Box2.setLength(12.0);
Box2.setBreadth(13.0);
Box2.setHeight(10.0);
// volume of box 1
volume = Box1.getVolume();
Console.WriteLine("Volume of Box1 : {0}" ,volume);
// volume of box 2
volume = Box2.getVolume();
Console.WriteLine("Volume of Box2 : {0}", volume);
Console.ReadKey();
}
}
}
当上面的代码被编译并执行时,它会产生以下结果 -
Volume of Box1 : 210 Volume of Box2 : 1560
C# 构造函数
类构造函数是类的特殊成员函数,每当我们创建该类的新对象时都会执行该函数。
构造函数与类的名称完全相同,并且没有任何返回类型。以下示例解释了构造函数的概念 -
using System;
namespace LineApplication {
class Line {
private double length; // Length of a line
public Line() {
Console.WriteLine("Object is being created");
}
public void setLength( double len ) {
length = len;
}
public double getLength() {
return length;
}
static void Main(string[] args) {
Line line = new Line();
// set line length
line.setLength(6.0);
Console.WriteLine("Length of line : {0}", line.getLength());
Console.ReadKey();
}
}
}
当上面的代码被编译并执行时,它会产生以下结果 -
Object is being created Length of line : 6
默认构造函数没有任何参数,但如果需要,构造函数可以有参数。此类构造函数称为参数化构造函数。此技术可帮助您在创建对象时为其分配初始值,如下例所示 -
using System;
namespace LineApplication {
class Line {
private double length; // Length of a line
public Line(double len) { //Parameterized constructor
Console.WriteLine("Object is being created, length = {0}", len);
length = len;
}
public void setLength( double len ) {
length = len;
}
public double getLength() {
return length;
}
static void Main(string[] args) {
Line line = new Line(10.0);
Console.WriteLine("Length of line : {0}", line.getLength());
// set line length
line.setLength(6.0);
Console.WriteLine("Length of line : {0}", line.getLength());
Console.ReadKey();
}
}
}
当上面的代码被编译并执行时,它会产生以下结果 -
Object is being created, length = 10 Length of line : 10 Length of line : 6
C# 析构函数
析构函数是类的特殊成员函数,每当该类的对象超出范围时就会执行该函数。析构函数与带有前缀波形符 (~) 的类的名称完全相同,它既不能返回值,也不能接受任何参数。
析构函数对于在退出程序之前释放内存资源非常有用。析构函数不能被继承或重载。
以下示例解释了析构函数的概念 -
using System;
namespace LineApplication {
class Line {
private double length; // Length of a line
public Line() { // constructor
Console.WriteLine("Object is being created");
}
~Line() { //destructor
Console.WriteLine("Object is being deleted");
}
public void setLength( double len ) {
length = len;
}
public double getLength() {
return length;
}
static void Main(string[] args) {
Line line = new Line();
// set line length
line.setLength(6.0);
Console.WriteLine("Length of line : {0}", line.getLength());
}
}
}
当上面的代码被编译并执行时,它会产生以下结果 -
Object is being created Length of line : 6 Object is being deleted
C# 类的静态成员
我们可以使用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("Variable num for s1: {0}", s1.getNum());
Console.WriteLine("Variable num for s2: {0}", s2.getNum());
Console.ReadKey();
}
}
}
当上面的代码被编译并执行时,它会产生以下结果 -
Variable num for s1: 6 Variable num for s2: 6
您还可以将成员函数声明为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("Variable num: {0}", StaticVar.getNum());
Console.ReadKey();
}
}
}
当上面的代码被编译并执行时,它会产生以下结果 -
Variable num: 3
