C # and Object-Oriented Programming Language

Source: Internet
Author: User

The second in series "Open the Door of C #"

II. C # and object-orientedProgramming Language

C # is a pure object-oriented programming language, which truly embodies the spirit of "Everything is an object. In C #, even the most basic data types, such as int, double, and bool, all belong to the system. Object type. In addition, the use of C # programming, there will not be anything that is a process that is not exclusive to the object. Therefore, to learn C #, you must have an object-oriented idea. If you do not understand the so-called "Object-oriented thinking", you cannot master the essence of C, it can only be limited to the surface of language characteristics.

Many books andArticleI like to compare object-oriented with process-oriented, but for learningProgramIt is completely unnecessary for beginners of design. A blank piece of paper is actually more conducive to future development than a full-text paper.

So what is "Object-oriented Thinking "? Its essence is of course the meaning of "Everything is an object. The development and design of software is centered on the development goal. For example, to develop an e-commerce website, we need to operate and care about e-commerce. Commodities, orders, users, directories, shopping carts, and so on are all objects. If you want to develop a forum, all the necessary elements in the forum, such as forum classification, topics, posts, comments, and users, are objects. From an early point on, we want to writeAlgorithmWhen it is used to calculate factorial, the starting value, calculation result, and the entire algorithm are all objects.

What are the characteristics of objects? Generally, an object should have attributes and behaviors. Taking 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, nationality, nationality, date of birth, and so on. The behavior can be walking, eating, running, playing games or playing football. From the perspective of word meanings, attributes tend to be in the scope of nouns, while actions are similar to verbs. However, objects also have special circumstances, or only have attributes or behaviors. For example, an ID card may only have attributes such as name, date of birth, nationality, and ID card number. For example, "fei" is an action, but it can also be considered an action.

However, this division is not absolute. According to the actual situation, the attributes and behaviors of the same object may be different. For example, it is also a "person". If we want to develop a household registration management system, we do not have to consider walking, eating, running, and other behaviors, but if it is a football game, then, the object "person" requires all kinds of soccer actions as the behavior of the object. For example, the "ID card" object may also add "isvalidate" for it for convenience.

It should also be noted that, in many cases, attributes are actually an object, such as name, which can be of the string type and belongs to sytem. object type. You can also create a class object for the name separately:
Public Class Name
Public String firstname;
Public String middlename;
Public String lastname;

In C #, there are various object types. In addition to the basic types provided, you can also Customize various types, including class, struct, Enum, interface, delegate, and event. In the previous example, the object "person" can be defined as the class type, and the nationality can be defined as the enum type. An ID card with no behavior can also be defined as a struct type. The action "fly" can be defined as the interface type. Delegate and event are special object types. For example, we can define a series of events for the Forum Management System, such as beforepost and afterpost. I will explain the content in detail later.

To master the object-oriented thinking, you must be able to recognize objects in the system. In the system we want to develop, which objects need to be defined as individual objects, and what attributes and behaviors should this object have are a headache for beginners. To identify objects correctly, you must first analyze the functions that the system should implement. For example, we need to develop an e-commerce system with the following functional requirements:
1. Product query is supported;
2. If you want to purchase a product, you need to become a registered user and log on to the product;
3. If the current stock does not have this item, the user can make a reservation;
4. the user can put the item into the shopping cart;
5. before purchasing a product, you must submit an order;
6. After the user submits the order, the system should send the order information to the user by email;
7. Users can add comments to products.

There is a simple way to help us identify objects, that is, to find the key terms in these function descriptions, such as products, users, shopping carts, orders, and comments. These words can basically cover the main objects of this e-commerce system. The attributes of an object can be used to evaluate the system's requirements and required information. For example, commodities must have commodity names, prices, related images, categories, and inventory. As for behavior, we can also find the verb associated with these object Nouns from the function description. For example, a query of a product indicates that the product has a search behavior. User Registration and logon indicate that the user has these two behaviors: Register and signin. Of course, the implicit behavior also includes signout.

To really identify objects, we may need to know the knowledge of UML and How to Write use cases. Or use extreme programming to write user stories and then identify them one by one. However, this knowledge seems too difficult for beginners. We now know that we can use this method. What I hope more is that after reading this section, the reader can establish the idea that "Everything is an object. Finally, I hope that you can regard the recognition object as a text-filling game. The key is not what the answer is, but that all the fun is in the game.

the object-oriented thinking is profound and profound, and can be clearly understood without a few words. However, building such an idea depends on your heart, which is a kind of "Wonderful understanding ". Maybe you haven't realized the true meaning of object-oriented after writing hundreds of small programs, but in the middle, you suddenly understand, there is a joy of opening up the cloud to see the moon. I didn't mean to show off xuanxu. Sometimes developing software really requires such inspiration. Some people say that programming is an art and I agree. So when will this kind of "Wonderful understanding" come? I cannot know the answer. This requires your understanding. However, this kind of understanding has nothing to do with talking about Zen. It's not just about daily meditation. It's still a matter of sureness. If you write more programs, you will understand it.

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.