C # object-oriented

Source: Internet
Author: User

The previous blog introducedC # language basics. When talking about C #, you have to mention "Object-Oriented". This blog will give a rough introduction to "Object-Oriented ". For details, see the figure at the end of the blog.

 

Class and Object

When it comes to object orientation, we have to mention "classes and objects". Classes are the abstraction of objects and objects are the instances of classes. The main members of a class include fields, attributes, and methods.

1. I personally think that "attribute" is a supplement to "field". It can be used for read/write control through get and set accessors.

2. The method is a function. Similar to the "process" in VB, the "sub process" is not returned. The "function" process is a natural process with a returned value. There is a "Constructor" in the method. Its function is to initialize the class. "Method overload" is an extension of the "method" function. On the basis of not changing the original method, it adds a function by declaring more than two methods with the same name, implement the same processing for different data types.

3. A class is an abstraction of objects, while an "abstract class" is an abstraction of classes. It provides an inheritance starting point. When designing a new abstract class, it must be used for inheritance. Naturally, abstract classes cannot be instantiated. abstract methods must be overridden by subclasses.

 

Three features

Currently, three main features of object-oriented are encapsulation, inheritance, and polymorphism.

1. Each object contains all the information it needs to perform operations. This feature is called encapsulation, so the object does not have to rely on the Object Handler to complete its own operations. A good program is "High Cohesion, low coupling", and good encapsulation can reduce coupling. Of course, it is too closed and not very good. Like a house, if the package is strict, there is no value, so the class must have a clear external interface.

2. inheritance is a "is-a" relationship. If such a relationship exists, it can be inherited. So why inherit? -- Reduce duplicates! It also naturally has disadvantages, because inheritance is a strongly coupled relationship, and as long as the parent class changes, the subclass has to change. C # does not allow many inheritance, so this function may be implemented through "interfaces ".

3. Another of the three major features is polymorphism. polymorphism indicates that different objects can execute the same action, but they must execute the same action through their own implementation code. The inherited polymorphism must be implemented through virtual method and method rewriting.

 

Multi-object processing

A program often does not process only one object, but many.

1. If you can determine the number of objects, the use of "array" is pretty good, the array is stored continuously in the memory, you can quickly traverse elements.

2. If you want to dynamically add as needed, the array will not be competent. Now you can consider the "set", which can provide the ability to add, insert, or remove a range of elements. However, the set is not type-safe, because no matter what type of data is stored, it is packed into object objects. If you call the set directly according to the original type, an error occurs and you must perform the unpacking operation, of course, this is troublesome.

3. in this case, the "generic" is displayed. It specifies the data or object type of its internal items during declaration and instantiation, this avoids the aforementioned type security problems and the performance problems of packing and unpacking.

 

Interface

The abstract class and inheritance mentioned above mentioned the interface. To put it bluntly, the interface is to abstract the local behavior of the class. If you only focus on the Behavior Abstraction, the interface is equivalent to the abstract class. When talking about interfaces, we have to mention their relationships with abstract classes.

1. abstract classes are abstract classes; interfaces are abstract behaviors.

2. If the behavior spans objects of different classes, the interface can be used. For some similar class objects, the abstract class can be inherited.

3. abstract classes are bottom-up; interfaces are top-down.

 


To better illustrate the above content, we should refer to the previous figure:

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.