(Teaching ideas C # object-oriented 2) preliminary understanding of the basic concepts of object-oriented

Source: Internet
Author: User

Based on the general understanding of the development history of computer language in the previous lesson, we already know that object-oriented is the historical development direction of computer language. Compared with machine language and assembly language, mostProgramAll are written in Object-Oriented or process-oriented languages. So what is object-oriented and process-oriented? What are their differences? Let's take a look at the process-oriented language.

 Process-oriented:

 

The process-oriented language uses the example of life to regard every task done by every type of person as a process of Luo Gan, for example, the following example describes a student's daily life and process-oriented language. It uses ''{......} ''Is to do everything. They are regarded as an integral part of a day's life.

In the process-oriented program world, what we want to do isFunctionThe function is composed of "()" and";. For example, "one-day student life ()" and "wash.

One-day life for students (){Thing-function}
{
Get up (); wash();Breakfast ();
School (); homework ();
Dinner (); sleep ();
}

 

week ?; Global variable {description --- variables and data }
Get up ( who? ) Format parameter
{< br> time; local variables
open your eyes (); climb up (); wear clothes ();
quilt (); bed ();
}

At the same time, the "get up ();" function in a day's life can also be seen as a separate process, which also has its own components, as shown in the following example: the wake-up function (process) also has several functions, such as "wearing clothes ();". Which components can be used in the process? Let's take a look at the variable of the function.

Variable:A variable can be viewed as a description of a function or data generated by a function. Generally, I like to say that variables are adjectives used to describe functions. In the above example, we have three variables: global variables, formal parameters, and local variables. What are their meanings? Let's take a look at global variables.

Global variables:Global variables are generally defined outside the function, and the descriptions of such variables within the function are common. For example, "week ?" It is a global variable. If we define Monday, students will live every day on Monday and get up at the same time on Monday, all functions that do not contain the Monday variable should occur on Monday, such as wearing clothes () and sleeping (); 

Local variables:A local variable is defined within a function. This variable is invalid for other functions that do not contain it. for example, if we define "time;" in the get up task (function) as, then the "one-day life for students" function is invalid, for functions inside the wake-up window, for example, "Open your eyes ();", we can open your eyes at 8 o'clock, for example, "Get up (); "It can also re-give the local variable" time "a value, such as getting up at 08:10.

Format parameters:Formal parameters are hard to understand and will be useful in the future. Parameters are stored in the "()" of function A. They are used to share a common and unchangeable description of the functions in function A. For example, when we get up () "In this method, a parameter is placed as" who?" Now we define "getting up (James)" in this way, then all we do in the process of getting up is what James did, and the process of a day's life () for students, james didn't do it because James is not a global variable. There can also be many parameters, such as getting up (who?,Location ?) Parameters are separated by commas.

I don't know if you understand this. I will ask a question about this question. Please give me an example of the process in real life and give three different variables ....... In fact, you only need to regard each task as a process. Even simple tasks can be divided into steps and understood as another function. But now let's take this example into consideration. If I want to define a life as a red day, we can define a global variable in the life of a day, or, the red light is a parameter for daily life.

If we want to describe the daily life of the workers and compare the Daily Life steps of the students, we will find that many of them are repetitive, but the workers do not do homework but work; if there are many repeated descriptions of the Teacher's Day, the difference between the teacher and the student is that the teacher is an assignment. to describe these three types of person's day with process orientation, you must write three such functions, these three functions are repeated in many ways, and if we add a function in this person's life, such as motion, we need to modify it in a large area, this undoubtedly increases the complexity of programmer programming and reducesCodeIn this case, the object-oriented programming concept emerged.
 
Object-oriented

 

First, a pairClass and ObjectClass is an abstract set of objects with the same attributes and functions. An object is a self-contained entity and is identified by a group of identifiable features and behaviors. Everything is an object, and an object is sometimes called a class instance. Let's use the example above to explain the class and object. Whether it's a student, a worker, or a teacher, they all have a thing in common that they are people, and people are a class, students, workers, and teachers are the three objects of the human class. These three objects have common characteristics and have their own characteristics. For example, students must learn and workers must work, the instructor must teach. For these three objects, they can also be three types: Student, work, and teacher. Primary school students are an object in the student class, and maintenance workers are an object in the work class, professor is an object of the teacher Class. Then, Xiao Ming in the first grade can be said to be an object in the primary school class. Xiao Ding in the shipyard is also an object of the Maintenance Engineer class ......, The difference between an object and other objects, and between a class and other classes is because, apart from the common points, the most important thing is to have their own characteristics and behaviors.

Class MemberIncluding attributes, fields, methods, indexers, and events. These knowledge points will be explained in detail in subsequent chapters. Let's take a look at the first three types of members today, all functions in C # are also called methods, variables are also called fields, and an attribute concept is added to protect fields. attributes can provide a way to value or assign values to fields, for example, the local variables in an object are clothes-wearing time. In the object-oriented process, other functions cannot be used. However, in the object-oriented model, attributes are provided to process (encapsulate) the local variables) when other classes or objects want to wear clothes, they can use this time through the attribute. What we need to remember now is that the field stores data, and the attribute protects the field by describing the class function, A large number of examples will certainly deepen your understanding of these words in the future.

Next, let's take a look at the object-orientedThree features: What do inheritance, encapsulation, and polymorphism mean?

let's take a look at encapsulation . Each object contains all the information required for operations, this feature is called encapsulation, so the object does not have to rely on other objects to complete its own operations. In this way, methods and properties are encapsulated in the class and implemented through the class instance (object. Encapsulation has many advantages. first, good encapsulation can reduce coupling (conflict). Second, internal implementation of the class can be freely modified. Third, class has a clear external interface. the benefits of encapsulation are well understood. For example, our house is a class instance (object). Interior Decoration and decoration can only be enjoyed and used by indoor residents, if there is no wall on all sides, all activities in the room will be shown in front of outsiders. With encapsulation, all the furnishings in the house can be changed without affecting others. However, if there are no doors and windows, a black box is wrapped strictly, even if its space is wider, there is no practical value. The doors and windows of a house encapsulate the properties and methods exposed by the object for people to access, as well as air circulation and sunlight.

with the definition of classes and objects in object-oriented languages, we can write the common content of the class "human" in our daily life into the class "human, when the daily life of the students, the daily life of the students, and the characteristics of the students themselves are added, the daily life of the students is defined, similarly, the daily life of a worker can also inherit the class. In addition, the daily life of a worker is also defined, and the daily life of a teacher can also be inherited. Back to the concept of inheritance , Object Inheritance represents a "is-", if two objects, A and B, can be described as 'B is A', it indicates that B can inherit from A. students, teachers, and workers are all human beings, so inheritance can be used. In fact, the successor can also be understood as the specificity of the successor, because it not only has the characteristics of the successor, but also has its own unique personality. Inheritance defines how classes are associated and shared. The inheritance method is to define the parent class and subclass, or the base class and derived class, where the subclass inherits all the features of the parent class. Subclass not only inherits all features of the parent class, but also defines new features. In the preceding example, the parent class is also called the base class, and the other three are subclasses of the parent class or derived classes. It is best to remember three sentences for learning inheritance. If the subclass inherits from the parent class , first, the subclass has the attributes and functions of the parent class that are not private, next we will talk about permissions. Next we will look at it. Second, sub-classes have their own attributes and functions, that is, sub-classes can expand the attributes and functions not available in the parent class; third, subclass can also implement the function of the parent class in its own way. This method is called method rewriting. we will discuss it later. Now let's take a look at using inherited modifications. If we do not use inherited modifications, We must modify them in all repeated methods. The more code, the more likely an error will be, the advantage of inheritance is that inheritance puts the common parts of all sub-classes in the parent class, so that the code is shared, this avoids repetition. You only need to modify the parent class when modifying it, and the other child classes are modified automatically. In addition, inheritance makes it easier to modify or extend the inherited implementations. However, inheritance also has disadvantages , that is, if the parent class is changed, the subclass has to be changed, we must take this into account when using inheritance. inheritance is considered only when the two classes have a "is-a" relationship.

as we mentioned earlier, the correct description should be access modifier , they can specify the access (use) permissions of other classes for members in this class. There are four types of access modifiers: Public, private, protected, and internal ). Public indicates that the modified class members can allow access by any other class, which is commonly known as public. Public access is the highest allowed access level; private indicates that only members in the same class are allowed to access the data. Other classes, including their subclasses, are not accessible, which is commonly known as private. If the member in the class does not have a modifier, it is considered private. Protected indicates that the Child class can have full access to the base class when inheriting, that is, the class member modified with protected, public to subclass, but not to other classes; internal keywords are access modifiers for type and type members. Internal types or members are accessible only in files of the same assembly. Let's give an example to help students understand the first three frequently used modifiers. My father spent his hard work in his life to buy a house. My father's house is in the Public shape. Because my father's friends, friends, and family can all play at home; the money Dad makes for his family is protected by protected. An outsider has no right to spend it, but his son can inherit it and turn it into his son's money; however, if Dad saves his own private money, such as the Social money used to play mahjong, his son cannot inherit it because his private property is private and private. Do you understand this? Access Permissions are frequently used. If you cannot find these Members when calling class members in the future, you must first consider whether the modifier of the members is proper.

Next we will look at the third feature:Polymorphism.Polymorphism indicates that different objects can execute the same action, but they must use their own implementation code. When learning polymorphism, pay attention to three points: first, the Child class appears as the parent class, second, the subclass is implemented in its own way during work. When the third subclass appears as a parent class, its unique attributes and methods cannot be used, which sounds hard to understand, there are also many ways to use polymorphism. Now we can't say it too deeply. We must combine the examples to make everyone understand it. Now you can think of polymorphism as an example, in a day's life, students may wear school uniforms and workers may wear work clothes. Although they wear clothes at the same time, the objects are different.

In this lesson, we compared the object-oriented and process-oriented languages. It can be seen that the object-oriented language is indeed more evolved and improved than the process-oriented language, this is achieved through years of programming experience. In fact, no matter how evolved the language, it is important to know how to use these features to write these codes more readable and easy to modify, today, many new concepts are introduced, so you don't have to worry about understanding the theoretical concepts before you can use them more freely. In this section, we begin to get started with actual code and understand these concepts.


  

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.