Chapter 1 classes and objects - define classes and create objects

1.1 Definitions class, create objects

In programming, to define the class, and then create objects (instances) of this class.

1.1.1 the definition of class

A grammar class is defined as follows:

Access modifier Class class name

{

    Members of the class 1;

    ................

    N-members of the class;      

}

The meaning is as follows.

1) "access modifier" is used to limit the scope or type of access levels, modifier such except only two kinds of public and internal (nested classes). Among them, declared as public classes can be any other type of access; access to other classes in the class declared as internal wisdom can only be set from the same program, that is, only the current project code to access. If the access modifier is omitted, the default is internal.

2) "class name" Naming of the same variable naming rules, using the Pascal class name naming conventions. Class names that reflect the functional class of nouns or noun phrases. Class file name individually defined to reflect the class content, preferably with the class of the same name.

1.1.2 members of the class

In the definition of class sinks, comprising members of the class field member, a member other methods. Field is used to describe the state of the members, the method for describing the operation member.

1. Fields

Class member variables, also known as field members, members of the field, also known as member variables in the following format.

Data Type Field Name access modifier = initial value;

1) If the other types of undesirable access to the member, when the members of the class definition, "access modifier" use the protected or Private; if desired other types of access to the member, the member in the definition of the class with public access character, for example:

public string name; // Name

2) "Field Name" Using the camel naming convention to name, using the definitions field names.

3) "initial value" represents an initial state of the field, for example:

public int age = 18; // age, integer, the initial 18 years of age

2. Methods member

Summary of the class definition, the class method referred to as method member in C #, called a function in other programming languages, format.

Access modifier return type   method name (parameter list)

{

      Method body;

       [return Expression;]

}

 1) member method described here, the "access modifier" can not be private private or protected protected, to be declared as public, in order to access to the other classes.

2) "return value type" may be a string, int basic data types, and may be a class type. If the cover method does not return a value, use the keyword void.

Take the form of a free parameter declaration, the method does not return a value:

public void  SampleMethod()

{

      Method body;

}

After the method finishes may not return any value, it may be a return value. If the method returns a value, the method must have a return statement body weight, and the return statement must specify the type of expression is consistent with the method returns a declaration; if the method does not return any values, the return type of void. Vivo can return statement, you can not return statement, the role of a return statement is executed immediately exit the method.

3) "method name" Pascal naming convention used, should be used verb or verb phrase. In one class, the same method of access modifiers or functions should be put together.

4) "parameter list" is a list of names and type names, the type can be a simple data type, class type may be.

Parameter list can not, may be a plurality. Even without parameters but also in the method name plus a pair of parentheses. Parameter list of the following form.

Type 1 parameter 1 , Type 2 parameter 2 , ...

The methods Member "parameter list" parameter is the parameter table (referred to as parameter Parameter), the parameters used in the definition is the process time, for the purpose of receiving an incoming call when the process parameters. Nature is a parameter name, do not take up memory space.

5) "method body" method is 0 or more statements.

 

 [ Example 1-1 using object-oriented thinking and abstract description of the student class.

Description: common student information basic information name, sex, age, class, etc., after a complete learning curriculum needs to take the exam, only to go to the next course of study after the pass. Please description, a student from the abstract object class. It requires the definition of the class students, student and instantiate an object in the main process.

The idea is as follows.

1) analyze the problem: Students learning courses.

2) extract objects: students.

3) analysis of the state of the object: name, gender, age, class and so on.

Operation 4) analysis of the object: learning, examinations.

5) the definition of class: students class Student.

status:

Name name

Sex gender

Age age

Class grade

operating:

Show learning courses Study (course), course curriculum name is displayed

Display Course Exam exam (course, score), course curriculum, score as

[ Workshop 1-2 ] use the object-oriented thinking and abstract description of the "Taiwan grass jelly milk tea chain" category.

Description: different "Taiwan grass jelly milk tea chain" have the same environment, tea variety, price and service, to display information in a number of tea shops.

 

[Example 1-2]   using object-oriented class thinking described rectangular parallelepiped.

Function Description: There are three rectangular edges, respectively called long cuboid, width and height. This rib 3 with a rectangular describe both, may be calculated rectangular parallelepiped volume, surface area.

The idea is as follows.

1) Analysis of the problem: with three ribs will be able to describe a rectangular parallelepiped, calculating the volume of a rectangular parallelepiped, the surface area.

2) refining the object: cuboid.

3) object state analysis: length, width and height.

Operation 4) Analysis of objects: calculated rectangular parallelepiped volume, surface area.

5) defining classes: Class cuboid Cuboid.

status:

Long length

Wide width

High height

operating:

Calculating the volume of a rectangular parallelepiped Cubage, cuboid volume = length × width × height calculating the surface area of ​​a rectangular parallelepiped TotalArea, the surface area of ​​a rectangular parallelepiped = (length × width × length + height + width × height) × 2

1.1.2 member variables

Member variable is a variable defined in the class, fields, properties can be called member variables. In the method defined variables are called local variables.

1. member variable with this keyword

If the name of a local variable with the same name member variables, in order to re member variables used in this method, you must use this keyword. In C #, the this keyword used in many ways, describes the use of the class definition herein, the this member variable for distinguishing and topical (local) variables (or parameters). This keyword is the name represents the class members defined in

【例1-3】定义一个名为Person的类,类中包括字段:name(姓名)、gender(性别)、age(性别),包括方法:Study()、Work()。代码如下。

上面Study()、Work()方法中的this.name表示成员名,name、age表示形式参数或局部(本地)变量。在方法中,通过this可以语义上区分成员名、参数名(或局部变量)。

注意:在实际编程中是不建议参数名与字段名相同的,这样做降低了代码的易读性,这里只是为了说明this关键字的用法而已。

2. 成员变量与局部变量的区别

1)  成员变量

1. 成员变量定义在类中,在整个类中都可以访问

2. 成员变量随着对象的建立而建立,随着对象的消失而消失,存在于对象所在的堆内存中。

3.  成员变量有默认初始化值

2)   局部变量

1. 局部变量只定义在局部氛围内,如方法(函数)内、语句内等,只在所属区域有效。

2. 局部变量存在于栈内存中,作用范围结束后,变量空间会自动释放。

3.  局部变量没有默认初始化值。

4.  在使用变量是需要遵循的原则:就近原则。首先在局部范围找,有就使用,接着在成员位置找。

3)    成员变量与局部变量的区别

1. 在类中的位置不同

成员变量:在类中方法外面

局部变量:在方法或者代码块中,或者方法的声明上(即在参数列表中)

 2. 在内存中的位置不同

成员变量:在堆中(方法区中的静态区)。

局部变量:在栈中。

 3. 生命周期不同

成员变量:随着对象的创建而存在,随着对象的消失而消失

局部变量:随着方法的调用或者代码块的执行而存在,随着方法的调用完毕或者代码块的执行完毕而消失。

 4. 初始值不同

成员变量:有默认初始值

局部变量:没有默认初始值,使用之前需要赋值,否则编译器会报错(The localvariable xxx may not have been initialized)。

1.3 创建对象

类是创建对象的模板,一个类可以创建多个对象,每个对象都是类类型的一个变量;创建对象的过程也叫类的实例化。每个对象都是类的一个具体实例(Instance),拥有类的成员变量和成员方法。

1.3.1 对象的声明与实例化

对于已经定义的类,就可以用它作为数据类型来创建类的对象,简称对象。创建类的对象分为以下两个步骤。

1.声明对象引用变量

类名 对象名;

1)  “类名”是已经定义的类名称。

2)  “对象名”使用camel命名规范来命名,使用名词命名对象名称

例如,声明一个Student类型的对象stu,代码如下。

Student stu ; //声明Student类的类型对象stu,但未实例化

上面代码,只声明了Student类型的变量,并没有对类中的成员赋值,即未实例化,类成员没有实例化将不能访问

2.创建类的实例

创建类的实例也称实例化一个类的对象,简称创建对象。可以对已声明的对象进行实例化(也称初始化),实例化对象需要使用运算符new。其语法格式如下。

 

对象名=new 类名(参数列表);

参数列表”是可选的,根据类的构造函数来确定。“参数列表”将在构造函数中介绍。

例如,下面代码创建Student类的实例,并赋给stu对象,对stu对象进行初始化,

Stu=new student();//创建Student类的实例,并把引用地址赋给stu

通常是把上面声明对象与实例化对象的两个语句合在一起,其语法格式如下。

 

类名 对象名=new 类名(参数列表);

例如,把上面两汉代码写在一起,代码如下。

Student stu=new Student(); //声明Student类型的对象,并初始化stu对象

使用类声明的对象,实质上是一个引用类型变量,使用运算符new 和构造函数来实例化对象并获取内存空间。

 

注意:有关类的实例(对象)的声明与创建不能放在该类的内部,只能在外部(即其他类中),通常写在class Program类的Main()方法中。

1.1.3  对象成员的访问

类或对象成员的访问,可分为在本类内使用该成员、实例成员的访问和实例方法的访问3种方式。

 1.  在本类内使用成员

如果在本类内使用成员,类名可以省略,用this表示本类内的成员。格式如下。

this.成员

this.成员

2.  实例成员的访问

如果在其他类中访问实例成员,格式如下。

对象名.成员名

说明:“.”是一个运算符,其功能是访问指定类型或命名空间的成员。

例如,下面代码为对象的成员赋值:

stu.gender=”女”; //为stu对象的gender成员赋值

3.  实例方法的访问

实例方法的使用格式如下。

对象名.方法名(实参列表);

在调用方法时,实际参数(简称实参argument)将赋值给形参。因而,必须注意实参的个数、类型,应与形参一一对象,并且必须要有确定的值。实参的本质是一个变量,并且占用内存空间。只有在程序执行过程中调用了方法,形参才有可能得到具体的值,并参与运算求得方法值。实参列表的形式如下。

 

实参1,实参2,…

 

使用带有返回值的方法使用时,调用实例方法的格式一般采用:

 

变量名=对象名.方法名(实参1,实参2,…….);

 

如果不需要使用方法的返回值,则采用如下调用格式:

 

对象名.方法名(实参1,实参2,……);

例如,下面代码:

stu.Study(“英语”); //访问stu对象的study()方法,实参是“英语”

【例1-4】在【例1-1】定义类的基础上,在Program类的Main()方法中编写如下代码。

【课堂练习1-5】请在【课堂练习1-2】定义类的基础上编写代码,在主程序中编写创建对象,给字段赋值,调用方法的代码。

【例1-6】在【例1-2】定义类的基础上,在Program类的Main()方法中编写如下代码。

 

Guess you like

Origin www.cnblogs.com/qy1234/p/12364401.html