当前位置:首页 > 《C#高级编程》中英文对照 - 类
Object and Types 对象和类型
WHAT'S IN THIS CHAPTER?本章内容:
? The differences between classes and structs 类和结构的区别 ? Class members 类成员
? Passing values by value and by reference 按值和按引用传送参数 ? Method overloading 方法重载
? Constructors and static constructors 构造函数和静态构造函数 ? Read-only fields 只读字段 ? Partial classes 部分类 ? Static classes 静态类
? The object class, from which all other types and derived Object类,其他类型都从该类派生而来
So far, you've been introduced to some of the building blocks of the C# language, including variables,data types, and program flow statements, and you have seen a few very short complete programs containing little more than the Main() method. What you haven't really seen yet is how to put all these together to form a longer, complete program. The key to this lies in working with classes ----- the subject of this chapter. Note that we cover inheritance and features related to inheritance in Chapter 4, \
到目前为止,我们介绍了组成C#语言的主要模块,包括变量、数据类型和程序流语句,并简要介绍了一个只包含Main()方法的完整小例子。但还没有介绍如何把这些内容组合在一起,构成一个完整的程序,其关键就在于对类的处理。这就是本章的主题。第4章将介绍继承以及与继承相关的特性。
This chapter introduces the basic syntax associated with classes. However, we assume that you are already famillar with the underlying principles of using classes ----for example, that you know what a constructor or a property is . This chapter is largely confined to applying those principles in C# code.
本章将讨论与类相关的基本语法,但假定你已经熟悉了使用类的基本原则,例如,知道构造函数或属性的含义,因此本章主要阐述如何把这些原则应用于C#代码
CLASSES AND STRUCTS 类和结构
Classes and structs are essentially templates from which you can create objects. Each object contains data and has methods to manipulate and access that data. The class defines that data and functionality each particular object (called an instance) of that class can contain. For example, if you have a class that represents a customer, it might define fields such as CustomerID, FirstName, LastName, and Address, which you will use to hold information about a particular customer. It might also define functionality that acts upon the data stored in these fields. You can then instantiate an object of this class to represent one specific customer, set the field values that instance, and use its functionality.
类和结构实际上都是创建对象的模板,每个对象都包含数据,并提供了处理和访问数据的方法。类定义了类的每个对象(称为实例)可以包含什么数据和功能。例如,如果一个类表示一个顾客,就可以定义字段CustomerID、FirstName、LastName和Address,以包含该顾客的信息。还可以定义处理在这些字段中存储的数据功能。接着,就可以实例化表示某个顾客的类的对象,为这个实例设置相关字段的值,并使用其功能。
Class PhoneCustomer {
public const string DayOf SendingBill = \public int CustomerID; public string FirstName; Public string LastName; }
Structs differ from classes in the way that they are stored in memory and accessed (classes are reference types stored in the heap; structs are value types stored on the stack), and in some of their features (for example, structs don't support inheritance). You will tend to use structs for smaller data types for performance reasons. In terms of syntax, however, structs look very similar to classes; the main difference is that you use the keyword struct instead of class to declare them. For example, if you wanted all PhoneCustomer instances to be allocated on the stack instead of the managed heap, you could write:
结构与类的是它们在内存的存储方式、访问方式(类是存储在堆(heap)上的引用类型,而结构是存储在栈(stack)上的值类型)和它们的一些特征(如结构不支持继承)。较小的数据类型使用结构可提高性能。但在语法上,结构与类非常相似,主要的区别是使用关键字struct代替class来声明结构。例如,如果希望所有的PhoneCustomer实例都分布上栈上,而不是分布在托管堆上,就可以编写下面的语句:
Struct PhoneCustomerStruct {
Public const string DayOfSendingBill = \Public int CustomerID; Public string FirstName; Public string LastName; }
For both classes and structs, you use the keywork new to declare an instance. This keyword creates the object and initializes it; in the following example, the default behavior is to zero out its fields:
对于类和结构,都使用关键字new来声明实例:这个关键字创建对象并对其进行初始化。在下面的例子中,类和结构的字段值都默认为0:
PhoneCustomer myCustomer = new PhoneCustomer(); //works for a class
PhoneCustomerStuct myCustomer2 = new PhoneCustomerStruct(); //works for a struct
In most cases, you'll use classes much more often than structs. Therefore, we discuss classes first and then the differences between classes and structs and the specific reasons why you might choose to use a struct instead of a class. Unless otherwise stated, however, you can assume that code presented for a class will work equally well for a struct.
在大多数情况下,类要比结构常用得多。因此,我们先讨论类,然后指出类和结构的区别,以及选择使用结构而不使用类的特殊原因。但除非特别说明,否则就可以假定用于类的代码也适用于结构。
CLASSES 类
The data and functions within a class are known as the class's members. Microsoft's official terminology distinguishes between data members and function members. In addition to these members, classes can contain nested types (such as other classes). Accessibility to the members can be public, protected, internal protected, private, or internal. These are described in detail in Chapter 5, \
类中的数据和函数称为类的成员。Microsoft的正式术语对数据成员和函数成员进行了区分。除了这些成员外,类还可以包含的类型(如其他类)。成员的可访问性是public、protected、internal protected、private或internal。第5章将详细解释各种可访问性。
Data Members 数据成员
Data members are those members that contain the data for the class ----fields, constants, and events. Data members can be static. A class member is always an instance member unless it is explicitly declared as static.
数据成员是包含类的数据——字段、常量和事件的成员。数据成员可以是静态数据。类成员总是实例成员,除非用static进行显式的声明 Fields are any variables associated with the class. You have already seen fields in use in the PhoneCustomer class in the previous example. 字段是与类相关的变量。前面的例子已经使用了PhoneCustomer类中的字段。
After you have instantiated a PhoneCustomer object, you can then access these fields using the object. FieldName syntax, as shown in this example:
一旦实例化PhoneCustomer对象,就可以使用语法ObjectFieldName来访问这些字段,如下便所示:
PhoneCustomer Customer1 = new PhoneCustomer(); Customer1.FirstName =\
Constants can be associated with classes in the same way as variables. You declare a constant using the const keyword. If it is declared as public , then it will in will be accessible from outside the class.
常量与类的关联方式同变量与类的关联方式。使用const关键字来声明常量。如果把它声明为public,就可以在类的外部访问它。
Class PhoneCustomer {
Public const string DayOfSendingBill =\Public int CustomerID; Public string FirstName; Public string LastName; }
Events are class members that allow an object to notify a caller whenever something noteworthy happens, such as a field or property of the class changing, or some
共分享92篇相关文档