类是对象的蓝图或模板,可以定义类来表示某种数据类型。这实际上并不定义任何数据,但它确实定义了类名称的含义。也就是说,该类的对象由哪个对象组成,哪些对象可以执行什么操作。 对象是类的实例。 构成类的方法和变量称为类的成员。
类定义从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)
{
// method1 body
}
<access specifier> <return type> method2(parameter_list)
{
// method2 body
}
...
<access specifier> <return type> methodn(parameter_list)
{
// method3 body
}
}
注意:
internal
。成员的默认访问权限是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
类的成员函数是在类中定义具有与其他变量类似的定义或其原型的函数。它对其所属类的任何对象进行操作,并且可以访问该对象的类的所有成员。
成员变量是对象的属性(从设计的角度),它们被保留为私有(private
)以实现封装。这些变量只能使用公共成员函数来访问。
为了理解上面的概念,我们从一个类中设置并读取另外一个类的成员的值:
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
类构造函数是当创建该类的新对象时执行的一个类的特殊成员函数。
构造函数具有与类完全相同的名称,它没有任何返回值。下面的例子解释了构造函数的概念:
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(100.0);
console.writeline("length of line : {0}", line.getlength());
// set line length
line.setlength(60.0);
console.writeline("length of line : {0}", line.getlength());
console.readkey();
}
}
}
当编译和执行上述代码时,会产生以下结果:
object is being created, length = 100
length of line : 100
length of line : 60
析构函数是一个类的特殊成员函数,只要其类的对象超出范围就执行。析构函数使用具有前缀波形符(~
)和类名称来表示,并且它不能拥有返回值,也不能使用任何参数。
析构函数在退出程序之前释放内存资源非常有用。 析构函数不能被继承或重载。
以下示例解释析构函数的用法,参考代码:
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(126.0);
console.writeline("length of line : {0}", line.getlength());
}
}
}
当编译和执行上述代码时,会产生以下结果:
object is being created
length of line : 126
object is being deleted
我们可以使用static
关键字将类成员定义为静态(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();
console.writeline("variable num for s1: {0}", s1.getnum());
console.writeline("variable num for s2: {0}", s2.getnum());
console.readkey();
}
}
}
当编译和执行上述代码时,会产生以下结果:
variable num for s1: 4
variable num for s2: 4
您也可以将成员函数声明为静态。这些函数只能访问静态变量。静态函数即使在创建对象之前也存在。以下示例演示了如何使用静态函数:
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