Chinaunix首页 | 论坛 | 博客
  • 博客访问: 180715
  • 博文数量: 12
  • 博客积分: 3000
  • 博客等级: 中校
  • 技术积分: 240
  • 用 户 组: 普通用户
  • 注册时间: 2007-08-04 15:43
文章分类

全部博文(12)

文章存档

2011年(1)

2009年(1)

2008年(10)

我的朋友

分类: 项目管理

2008-08-01 10:23:33

 

1.    Objects and Classes

 


1.1            what is an object

Before talking of the meaning of object in software field , we can firstly mention  meaning of object in real life .According to Merriam-Webster’s Collegiate Dictionary (韦氏大字典)the explaination of object involves two items:

1.      some substance people can be felt by sensorial organ

2.      some concept kept in mind

 

The explaination can also be used in software field, for instance,in SRS(Student Registeration System),some object we must consider are following:

 

l        student

l        lecturer

l        classroom

l        textbook

we can regard objects above as some substance we can feel by our sensorial organ

 

l        course

l        grade

l        feedback

these objects can be seen as some concept,which we keep in our mind

 

In summary, software objects is an software struction,which composed with some state(data) and behavior(operation) and is used to describe some objects in real life.

 

1.2            State / Attribute / Data

To record a student’s information , following are needed:

l        student name

l        ID No.

l        birthday

l        address

l        major in research

l        GPA

l        faculty advisor

l        course load

 

whar about a course?

l        course No.

l        course name

l        duration

l        lecturer

As we disscusion above, something we use to describe data of an object is called arrtibute.(字段)

When all attributes get together,they response to an object’s state.

 

1.3            Behavior / Operation /Method

Take student object for example, behavior of a student involves:

l        register a course

l        drop a course

l        choose major

l        choose advisor

l        provider GPA

We call all behavior opreation,which gives some methods  used to get and set(or change) attribute..

 

1.4            Class

“Class” of some abstracted model describe some similar features of a group of objects, for example, a class named “Student” is used to response to all sutudents SRS.

 

 

A class involves following elements:

l        attribute (name and type)

l        operation (how can an object execute some operations)

 

                 Suggested Attribute of Student Class

AAttribued

            DataType   

name

            string

id

string

birthday

           DateTime

address

string

major

string

gpa

            double

As we suppose, every student must have these nine attributes. As for operations,there must be five items:

l        RegisterForCourse

l        DropCourse

l        ChooseMajor

l        ChageAdvisor

l        PringTranscript

The folloing parts will introduce how to determine attributes and operations of an object by analyzing system’s need.

 

1.4.1        Something about Caseing (命名法)

 

Speak for myself, I hav a sepcial fany for a casing way called “Pascal Casing” (也叫驼峰命名法),following are some examples for attributes:

 

 Student / StudentID/ StudentName/ CourseCategory

 

1.4.2        Instantiation(实体化)

 

Class can be seen as a model to create many object ,which is used to:

l        store all attribute of a new object in some area of memory

l        link a range of operations and objects

Instatiation is a process that a new object is created.If a class has been declared ,many objects can be created by this class,just like we can product a lot of shoes by a shoes model.The relation between class and object can be summarized as following:

l        in a class,atrribute and method was defin, a class can be seen as a template(模板)

l        object is a instance of a class,and all attribute have been given some data

 

1.4.3        Encapsulation(封装)

 

Encapsulation is a terminology(术语) we must know when we learn object-oriented,however ,encapsulation is not something special in softeware field.Object-oriented has improve encapsulation. If you have studied C language previously, you must know stuct which is alos a example of encapsulation.

If you have no experience about that ,following will explain what is encapulation:

Put all state and operation together within logical unit,and give user a good way to operate a unit without how it execute.

 

1.5            User-defined Type and Reference Variable

As we know , int , char ,double…all these are some usual type in C# language, which has been defined previously by C# desinger. But C# also provide a mechanism to user to define some data type by themsleves under the help of class.

To be more specifical, a class is indeed a datatype,eg:

Studeng  y;

This sentence declares a object y, Student is a user-defined type , we can use it just like int char,doubel….and then we call y a reference variable, it is the same with other varialbe,expect that they have their special operation but not + - */.

 

1.6            Deep understand of Instantiation

As we dicussed above ,

Studeng  y;

This sentence declares a object y, and Student is a class.But a object isn’t really created at this time .It just declared a reference variable,and this variable will refer to a Student object, meanwhile, hasn’t at this time.In other words, it refers to NULL because the real object dosen’t exist now.

We must use a special operation new to create a real Student object,and then link this objcet to reference variable y:

      Y= new Student( );

Reference variable sometimes can be seen as a link to a real object ,and we can call this link handle(句柄). It can be alos understanded that a reference variable is to hole a object’s handle.The following picture can demonstrate it well:

 

 

 

Specially ,we can create a object without giving handle to any reference variable,such as:

new Student( );

In this case ,this real object dose exist in memory,but it can not be operated in our program,it is out of our master.

Another way to initial reference variable is displayed as following:

Student X, Y;

Y=new Student( );

X=Y;

 

 


Picture above illustrate the result of sentences,as we can see from it,a certain object give its handle to reference variable X andY at the same time. But ,a reference variable just can hold only one object’s handel at sometime.

 

In addition,if all handle of a object is droped by reference variables,as said before,it can’t be operated any more and will be elimated at last in memory(Only in C#).

We must pay attention to this special case only in C#,if you have some sence of C or C++,you must know memory releasing is often forgetted and will bring a lot of troubl.In C#, there is a special mechanism called CLR(common language runtime公共语言运行时) which will clear up all rubbish when it can’t be used any more.

 

1.7            Objects used as an attribute

The attribute we discussed previously only involves some usual data type(int ,char double…).However ,in some cases,we will use some user-defined type to define some attribute.For instance ,if we add advisor to a Student object’s attribute,we can’t use usual data type to define it:

Suggested Attribute of Student Class

AAttribued

            DataType   

name

            string

id

string

birthday

           DateTime

address

string

major

string

gpa

            double

advisor

              ???

 

We will define a Professor class, and define advisor’s type Professor by making advisor attribute a reference variable. This will give a Professor object’s handle to

 a Student object.

 

1.8            Composition(合成)

When define a class,one or some attriubte of this class may refer to some other object,which we call it “Composition” as an Object-oriented mechanism.There is no limit of step relation (嵌套关系)among different objects,and compositon can really help us to model a complicated real life.

When we make use of composition ,we just inert an object into another object.There are also some complex cases, for example a Student object A and a Professor objectB, it can be easily known that A need a handle refering to B,B also need a handle refering to A.In fact ,we do not store the whole object in another ,we just store a reference variable to refer to object.These two object indeed exist,and we can kuickly find them when they need interaction.

 

阅读(913) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~