1,Class的概念
Class其实就是数据和函数以及描述的集合,它包含以下内容:
1.1. Constants --- 常数
例:
public const double GRAVITY = 32;
1.2 Enumerations 枚举量,Sets of named value
例:
public enum DistanceUnits
{
inches, feet, yards, miles
}
四个枚举量内部都赋值为整数0,1,2,3,所以等同于以下声明。
public enum DistanceUnits : int
{
inches =0, feet=1, yards=2, miles=3
}
也可以得到希望的其他内部值:
public enum Direction : short
{
North - 0x01,
East = 0x02,
South = 0x04,
West = 0x08
}
1.3. Fields, or data elements --- Data stored in an object,隐藏在object里面的数据
e.g.
private readonly double accel;
1.4. Functions or method --- Methods that act on an object,也就是对于object的操作
1.5. Constructors(instance and class) --- Special functions that initialize and object. 构造函数,每当使用class(例如instance)的时候,首先调用constructor初始化。
. Property --- Smart fields, or methods that act like data fields. 特别数据,称为property(财产)
例:
Properties 为C#特有,用于构造smart field
Public double Acceleration
{
get
{
return accel;
}
set
{
accel = value;
if (accel > 10.0)
accel =10.0;
else if (accel < -10.0)
accel = -10.0;
}
}
这里双精度变量Acceleration被限制了数值的范围区间-10.0 ~~~10.0,有点像是非线性函数。
. Indexers --- Special functions that let you treat an object like an array. (习惯使用数组的人可以把object作为数组使用)
. Operators --- Special functions that redefine how operators work on objects.(许多熟悉的操作其实都是函数,如+,-,*,/)
. Events --- Special function that raise events. (事件也是函数)
.Other classes --- Classes 可以嵌套(函数不可以嵌套)
2,Access 分类
对于Class的操作限制范围窄有很多好处,其中之一就是模块化,每当修改一个模块,其他模块不受影响。
. public --- Accessible anywhere
. internal --- Aceessible only to other classes the the same assembly. 源程序编译之后不是可执行代码,而是转换成assembly,装载之后在run time才可以变成可执行代码。所以这里说是same assembly. 他的范围比public窄,但比其他的要宽了。
. protected --- 受保护的,只能在它derived classes里面用,范围也比较宽了。
. protected internal --- Accessible to derived classes and other classes in the same assembly.
. private --- 使用范围最窄的class, 只能在本地class里面用
转载于:https://www.cnblogs.com/veritasfx/archive/2006/12/08/586043.html