C # Learning Notes (vi)--Introduction to object-oriented programming

Source: Internet
Author: User

First, the meaning of object-oriented programming

* is a modular programming method, so that the reuse of code greatly increased.

* OOP technology makes the design phase of a project much more energetic, but once a certain type of data expression is agreed upon, this expression can be used until the end of the life cycle of the application.

(i) Meaning of the object

1. An object is an integral part of the OPP application, which encapsulates parts of the application, which can be a process, some data, or some more abstract entity. The type of the object has a special name in OOP called a class. You can use a class to define an object.

PS: The term "class" and "object" are often confused, but simply put, the class is a template, and the object is a template made out of the product

(We will use a Unified Modeling language UML syntax to study classes and objects)

For example, printer and Myprinter to differentiate classes and objects with UML

2. Attributes and Fields

(1) Data contained in an object can be accessed through properties and fields. This object data can be used to distinguish different objects, because different objects of the same class store different values in the properties and fields.

(2) Properties and fields are different because properties do not provide direct access to the data.

(3) UML diagram

A, accessibility: The + sign represents a public member, the-number represents a private member.

B, member name

C, type of member

2. Methods

(1) UML diagram

A, whose syntax is similar to fields and properties, but the last type shown is the return type, in which the wind summary also shows the parameters of the method.

b, in, out, and inout they are used to represent the direction of the data flow, where out and inout roughly correspond to the C # keywords out and ref discussed in the study Note (v). In roughly corresponds to a scenario in C # that does not apply these two keywords.

3. All objects

4, the life cycle of the object

(1) Two important stages:

A, construction phase: the period at which the object was initially instantiated. This initialization process is called the construction phase, which is done by the constructor.

B, destructors: When you delete an object, you often need to do some cleanup work, such as freeing up memory, which is done by the destructor.

(2) Constructor:

The initialization of an object is done automatically, but in the process of initializing the object, the data stored by the object needs to be initialized. Constructors are functions that are used to initialize data.

All classes include more than one constructor in these functions: there is a default constructor, which has no arguments, and the name is the same as the class name. The class may also include several constructors with parameters, called non-default constructors. The code allows them to instantiate objects in many ways.

In C #, the constructor is called with the New keyword.

constructors, like fields and properties, can be either common or private. External code outside the class cannot use a private constructor to instantiate an object, but must use a public constructor.

(3) destructor

You should not rely on destructors for the functions used by the study object instance, because after an object is no longer used, the resource is occupied by that object for a long time. This can be problematic if the resources used are important. (There will be a solution in the back).

4. Static and instance class functions

(1) Classes also have static members, which can be shared among instances of a class, and do not even need to instantiate objects when using static members.

UML Diagram notation:

In a UML diagram, a static member of a class is represented by an underscore.

(2) Static constructors

When you use static members in a class, you need to initialize those members beforehand. This initialization task can be performed with a static constructor, and a ray can have only one static constructor, but the constructor cannot have an access modifier or take any arguments.

Static constructors cannot be called directly and can only be performed in the following cases:

A, when you create a class instance that contains a static constructor

b, when accessing static members of a class with static constructors

(3) Static class

If we want the class to contain only static members, a simple way is to use a static class instead of setting the class's constructor to private, and static classes can only contain static members.

C # Learning Notes (vi)--Introduction to object-oriented programming

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.