(original) C # learning note 08--Introduction to Object-oriented programming 01--the meaning of object-oriented programming 01--the meaning of an object 01--properties and fields

Source: Internet
Author: User

Chapter eighth Introduction to object-oriented programming

The contents of this chapter:
What is object-oriented programming
OOP technology
The Windows forms application's dependency on OOP

8.1 Meaning of object-oriented programming

The programming method described earlier, called function (or procedural) programming, often leads to so-called single applications, where all functions are contained in several code modules (often a code module). with OOP, many code modules are often used, each of which provides specific functionality, each of which is isolated and even completely separate from other modules. This modular programming approach provides a very large variety and greatly increases the chances of reusing code .

OOP technology is based on structure, the meaning of data, and the interaction between data and data. This usually means putting more effort into the design phase of the project, but the scalability of the project is relatively high.

In addition to consistency in the way data is expressed, OOP programming can often simplify tasks because the structure and usage of more abstract entities are consistent.

Meaning of the 8.1.1 object

To put it simply, objects are very similar to the structure types discussed earlier in this book, including variable members and function types. It contains variables that make up the data stored in the object, which contains functions that can access the functionality of the object. A slightly complex object may contain no data, but only functions, representing a process.

Objects in C # are created from types, just like the previous variables. The type of the object has a special name in OOP: Class. you can instantiate an object with the definition of the class, which means that an instance of the class is created. "Instances of classes" and objects mean the same thing, noting that "classes" and "objects" are completely different concepts (for example: type: car; Examples: Land Rover, Audi A6, BMW X5, etc.).

This chapter uses the Unified Modeling Language (Unified Modeling language,uml) syntax to study classes and objects. UML is designed for application modeling, from the objects that make up applications, to the operations they perform, to the use cases we want .

Figure 8-1 is a UML representation of the printer class printer. The class name appears at the top of the box (the following two areas are discussed later).

Figure 8-2 is an example of this printer class Myprinter.


At the top, the instance name is displayed in front, followed by the class name. The two names are separated by a colon.

1. Properties and Fields

  You can access the data contained in an object through properties and fields. This object data can be used to distinguish between different objects, because different objects of the same class store different values in the properties and fields (for example, everyone is an object and has attributes: the eye degree. Each person's eye degree is different, but people are all the same type. That is, each object is independent. (except for static data).

  The different data contained in the object forms the state of the object . Suppose an object class represents a cup of coffee called a cupofcoffee. When instantiating this class (that is, the object that created the class), you must provide a state that is meaningful to the class. You can use properties and fields so that the code can set the coffee brand to use, whether the coffee is milk or sugar, and whether the coffee is dissolved. As a result, the given cup of coffee has a specified state, for example, a Colombian drip coffee with milk and two cubes of sugar.

Fields and properties can be typed, so information can be stored in fields and properties, as string values, int values, and so on. However, properties are different from fields because properties do not provide direct access to the data. objects let the user not consider the details of the data, and do not need to be represented in a one-to-one manner.

  in general, it is best to provide properties , rather than fields, when accessing the state, because this gives you more control over the behavior, which does not affect the code that uses the object instance, because the syntax for using attributes and fields is the same.

Read-write access to a property can also be explicitly defined by the object. Some properties are read-only and can only view their values, not change them (at least not directly). You can also have write-only properties that operate in a similar manner.

You can also specify another type of access permission, called accessibility, for fields and properties. This can be
Accessibility determines what code can access these members, whether they are available for all code (public) or only for code in a class (private), or more complex patterns (see later in this chapter). The common case is to set the fields to private and access them through public properties (so that the external can only modify the value of the object through public methods to avoid passing in invalid values).

  In the UML representation method of the class, the second section shows the properties and fields , as shown in 8-3.


Each member contains the following information:

accessibility, + number represents a public member,-the number represents a private member. However, in general, private members are not shown in the diagrams in this chapter because they are information inside the class. As for read/write access, no information is provided.

The member name.

The type of the member.

Colons are used to delimit member names and types.

(original) C # learning note 08--Introduction to Object-oriented programming 01--the meaning of object-oriented programming 01--the meaning of an object 01--properties and fields

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.