Knocking on the door of C # and the Object-oriented programming language

Source: Internet
Author: User
Tags date key object object query require
Programming | Object C # is a pure object-oriented programming language, it really embodies the spirit of "everything is Object". In C #, even the most basic data types, such as the Int,double,bool type, belong to the System.Object type. In addition, using C # programming, there is no such thing as a process that is not part of the object. Therefore, learning C #, you must have object-oriented thinking, do not understand the so-called "object-oriented thinking", it is impossible to grasp the essence of C #, and the understanding of C # can only be limited to the surface of the language features.

Many of the books and articles on object-oriented thinking prefer to compare object-oriented to process oriented, but it is not necessary for beginners to learn programming. A blank paper, in fact, is more conducive to the future development than the paper full of words.

So what is "object-oriented thinking"? The essence of course is the meaning of "everything is the object". Software development and design, is around the development of the goal. For example, the development of an E-commerce site, then related to E-commerce are we want to operate and care, goods, orders, users, catalogs, shopping carts and so on, are objects. If you want to develop a forum, then the necessary elements in the forum, such as forum classification, themes, posts, Comment, users, and all objects. From the beginning, we asked to write an algorithm to calculate the factorial, then the starting value, the result of the operation, and the whole algorithm are objects.

So what are the qualities of an object? Generally, an object should have properties and behavior. Take the real world as an example, "man" as a special animal, is also called "Object". This object has many attributes, such as name, height, weight, ethnicity, nationality, date of birth, and so on. Behavior can be walking, eating, running, playing games and playing football. From the point of view of semantics, the attribute is more inclined to the category of noun, while the behavior is close to the verb. However, objects also have special conditions, either with attributes only or with behavior. For example, ID card, may only have the name, birth date, birthplace, identity card number and other attributes, but no behavior. For example, "flying", which is an action in itself, can also be considered an act.

However, this division is not absolute, according to the actual situation, the same object has the properties and behavior may be different. For example, the same is "people", if it is to develop a household registration system, we do not have to consider walking, eating, running and other acts, but if it is a football game, then the object of "people", you need all kinds of football action as the object of behavior. For example, "id" object, may also be used for convenience, will add "isvalidate" such an action.

Also note that many times the attribute itself is also an object, such as a name, can be a string type, it belongs to the Sytem.object object type, and we can create a separate class object for the name:

public class Name
{
public string FirstName;
public string MiddleName;
public string LastName;
}

In C #, there are various types of objects that support custom types, including Class,struct,enum,interface,delegate,event, in addition to the basic types provided by itself. In the previous example, the object person "human" can be defined as the class type, and the nationality can be defined as the enum type. An identity card that has no behavior can also be defined as a struct type. The action "Fly" can be defined as the interface type. As for delegate and event, it is a special type of object, such as we can manage the system for the Forum, define a series of events, such as Beforepost,afterpost and so on. I'll explain this in more detail in the following sections.

To master the object-oriented thinking, we must have the ability to identify the objects in the system. In the system we are developing, what needs to be defined as separate objects, and what attributes and behaviors this object should have, is a headache for beginners. To properly identify an object, you need to first analyze the functionality that the system should implement. For example, we require the development of an e-business system with functional requirements as follows:

1, support the product query function;

2, users need to buy goods, to become registered users and login;

3, if the current inventory does not have the goods, users can book;

4, the user can put the goods into the shopping cart;

5, the user buys the commodity before, needs to submit the order;

6, the user submits the order, the system should send the order information to the user;

7, users can add comments to the product.

A simple way to help us identify objects is to find key terms in these functional descriptions, such as goods, users, shopping carts, orders, reviews. These words can basically cover the main object of this e-business system. As for the object's properties, we can examine the system's requirements for this object and the information needed. For example, goods, must require a commodity name, price, related pictures, categories, inventory and so on. As for behavior, we can also look for verbs associated with these objects from the functional description. For example, a product query, on behalf of the product has search behavior. The user registers with the login, on behalf of this user has these two kinds of behavior: register,signin. Of course, the implied behavior of nature also includes SignOut.

To really identify a good object, maybe we need to master the knowledge of UML and know how to write use cases. Or use the extreme programming method, the user story written out, and then identified. But this knowledge seems too difficult for beginners. We now know that there is a way to do it. I hope that readers will be able to establish the idea of "everything is the object" after reading this section. Finally, I hope that we can recognize the object as a crossword puzzle, the key is not what the answer is, the fun is all in the game.

Object-oriented thinking is broad and profound, not words can be said clearly. But to build such an idea, it has to be in your heart, and this is with a kind of "wonderful realization". Perhaps after you have written hundreds of small program has not been able to realize the object-oriented truth, but in an instant, you suddenly understand that there is a way to open the clouds to see the moon bright joy. I don't mean to show off Psycho, sometimes it really takes a little inspiration to develop software, some people say, programming is an art, I agree. So when will this "wonderful enlightenment" come, I cannot know the answer, which requires your understanding. But this kind of understanding has nothing to do with Shanshan, not every day meditation can, or down-to-earth point, write more procedures, slowly you will understand.

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.