"You must know. Net" chapter 1st Study Notes

Source: Internet
Author: User

The basic concepts of object-oriented include class, object, inheritance, encapsulation, and polymorphism.

The birth of an object only initializes required fields, and other data must be completed through subsequent operations, such as assigning values to attributes and obtaining necessary information through methods.

The direction can be divided into value type and reference type. The classification is based on the position in the runtime, the value type is in the thread stack, and the reference type is in the managed heap. There can be type conversion between the two. This process is called packing and unpacking.

The use of an object refers to the process in which the method in the called object interacts with the object name to change the object state information.

The object's survival environment is CLR. CLR provides a hosting environment on which the object depends to survive, and develops a series of rules, called syntaxes, to establish a real order in the world of name. Object-Oriented Programming abstracts changes through interfaces to form a system.

Access modifier:

Public: no access restriction for members (undirected)
Protected: only the current class or the derived class of the current class can be accessed (vertical)
Internal: access is limited to the current Assembly (horizontal)
PRIVATE: access is limited to the current class (point direction)
Protected internal: intersection of protected and internal (vertical and horizontal)

The same method achieves different operations under different circumstances, which is the charm of polymorphism. There are two main types of Object Name polymorphism: 1. Interface implementation; 2. abstract class implementation.

GC manages all managed objects. When memory recycle is executed, GC checks the objects that are no longer used in the managed heap (no referenced objects) and executes internal recycle operations.

About inheritance:

Inheritance is one of the three elements of object-oriented (the other two are encapsulation and polymorphism). It is the relationship between classes and classes in Object-Oriented. through inheritance, subclasses obtain members of parent classes, child classes can also expand members of the parent class. The Inheritance Mechanism reflects the reusability, scalability, and security of object-oriented technology, which is the basic technology of object-oriented development and module development.

Inheritance can be classified as follows: 1. Single inheritance is implemented; 2. Multiple Interfaces are inherited.

For the visibility of inheritance, in. net, access permissions (that is, access modifiers) are used for control.

Implementing Virtual Methods in the parent class in subclass is called dynamic binding of methods. This is another feature of object-oriented-the basic mechanism of polymorphism.

Advantages of interface inheritance: through interface inheritance, object and behavior are separated,

Inheritance makes it easy for us to reuse and expand Code. At the same time, we can overload (different parameters with the same name are implemented) and overwrite (same name and different parameters are implemented ), interface implementation and other methods to encapsulate changes, hide private information and other basic object-oriented rules.

There are two main ways to extend and rewrite the parent class in the subclass: 1. Add a new method (Extension) to the subclass; 2, rewrite (rewrite) the method in the parent class)

When an object is created, the fields of its parent class at the highest level are first created, the parent class fields at other levels are created in sequence, and its own fields are created. Second, create a method table. The method table is created when the class is loaded to appdomain for the first time, when an object is created, only the typehander of its appended Member points to the address of the method list on loaderheap and associates the object with its dynamic method. Therefore, there are methods tables and objects. The method table is created after the parent class first subclass.

Here is a few points:

Inheritance can be passed
Subclass can call the methods and fields of the parent class. Otherwise
Virtual Method for overwriting
The subclass not only inherits the public members of the parent class, but also the private members of the parent class, but cannot be accessed in the subclass.
Blocking Effect of New keywords in virtual method inheritance

Follow the object principle: Calling subclass or parent class methods depends on whether the created object is a subclass object or parent class object, rather than its reference type.
Execute the proximity principle: For fields or methods of the same name, the compiler searches for them in order to reference them, that is, access the fields that are close to their creation.

Implement inheritance and interface inheritance:

Abstract classes are suitable for inter-class relationships with the concept of the family layer, and interfaces are most suitable for providing general functions for different classes.
The interface focuses on the can-do relationship type, while the abstract type focuses on the IS-A relationship.
Interfaces are mostly used to define the behavior of objects, and abstract classes are mostly used to define the attributes of objects.
If a version issue occurs during reservation, you can create an abstract class.
The value type can only implement interfaces and cannot inherit classes.

Aggregation or inheritance?

The inheritance relationship has a high Coupling Degree.
The aggregation relationship can be divided into three types: None, share and composite, the coupling degree is improved once, but there is still no inheritance of High, it can be said that coupling is a kind of HAS-A relationship.
Dependency indicates that when class 2 is modified, class 1 is also affected.

The basic principles of object-oriented programming are: Multi-aggregation, less inheritance, low coupling, and high cohesion.

The inherited high Coupling Degree will bring some adverse reactions to programming, avoiding methods such as separating objects and behaviors from abstract interfaces, and replacing inheritance with aggregation to implement more flexible subclass requirements.

Adapter mode introduction:

The adapter mode is used to convert an interface of a class to another interface, so as to meet new requirements without changing the original system, the new adapter class is introduced to expand and transform the existing storage system.
The main implementation method of adapter mode:
1. Adapter mode of the class: Introduce a new type to inherit the original type and implement the newly added interface method.
2. Object Adapter mode: extends the original system through aggregation.

Some so-called rules:
The sealing class cannot be inherited (what is the sealing class)
The inheritance relationship focuses on commonalities. commonalities are the basis of hierarchical multiplexing, while polymorphism focuses on features, which are the basis of system expansion.
Implement single inheritance with multiple interfaces
The basic principles of object-oriented programming are: Multi-aggregation, less inheritance, low coupling, and high cohesion.
Control the number of inherited Layers

Inheritance is one of the basic technologies of the. NET operating mechanism. Everything is an object and everything is inherited.

"Understanding inheritance, focusing on encapsulation, taste polymorphism, and playing interface" is the starting point for understanding object-oriented.

Encapsulation:

Encapsulation is one of the three elements of object-oriented design. It provides the implementation means for the interaction between systems, modules, classes and classes. Encapsulate the specific implementation of the hidden class and provide a unified access interface to operate internal data members.

Generally, the core nouns described in system requirements can be abstracted as classes, and the actions driven by these nouns are abstracted as methods.

Field: usually private, indicating the status of the class. The first principle of encapsulation is that the field is defined as private.
Attribute: Usually public, indicating the external member of the class. The get and set accessors are used to control the read and write operations on properties. In essence, the GET _ attribute name and SET _ attribute name are automatically generated.
Indexer: it is a property with parameters.
Method: A method represents a class behavior. behaviors can be divided into private and public. External behaviors provide interfaces for external interaction. Another principle of encapsulation is that effective protection of internal data is as important as effective exposure of external behavior.

Encapsulation principles:
Call the class accessors instead of members as much as possible.
The internal private part can be changed at will, but the external interface must be stable.
Use attributes to implement read and write Control Over Fields
Class encapsulation is usually used to control access permissions.
The essence of encapsulation is encapsulation changes, which encapsulate the frequently changed parts of the class into independent parts, which is conducive to full software reuse and system flexibility.

Polymorphism:

Polymorphism refers to the mechanism by which different instances of the same operation produce different running results. Polymorphism is also one of the three elements of object-oriented.

Polymorphism can be divided:
Base class inheritance, declare a virtual method in the parent class, can have different implementations in different subclasses (IS-A type)
Interface implementation, specifying the implementation of methods in sub-classes in the way agreed by the interface, with higher flexibility (Can-Do type)

Multi-state running mechanism:

The dynamic binding mechanism of. Net achieves the object-oriented polymorphism mechanism.
Static binding determines the association during compilation. It is generally implemented by method overload.
Dynamic binding, also known as late binding, is a method for determining dynamic association by checking the virtual method table during running. Generally, the inheritance and virtual methods (virtual) are used to override (override) in the subclass) virtual method. Extract the features that are common but easy to change in the subclass into a virtual method and define them in the parent class. Rewrite them in the subclass.

Interface:

An interface is the key to opening the door of design. An interface is a constraint that indicates what can be done (Can-Do). Different implementation objects can implement the same operation in different ways.

The interface-based design has made many great design patterns in the object-oriented thinking.

An interface is essentially a class that defines sub-abstract methods. This class only provides the definition of methods, and its implementation is completed by the Implementation class of this interface. The implementation of interface methods in the implementation class is actually an override ). Therefore, in a sense, an interface is essentially a class that cannot be instantiated, and its implementation mechanism is polymorphism.

Interface principles:
Interfaces should be small interfaces with a single function, and class behavior extension can be realized through the combination of interfaces.
Do not add new members to published interfaces.
The interface cannot be instantiated. The interface member is implicitly public.

A good program is oriented towards abstract programming rather than specific programming.

Conclusion

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.