First, Package:
Encapsulation is the first step in implementing object-oriented programming, in which a collection of data or functions is set in a unit (what we call a Class). Encapsulated objects are often referred to as abstract data types.
The meaning of encapsulation:
The meaning of encapsulation is to protect or prevent the code (data) from being accidentally destroyed by us. In object-oriented programming, data is considered to be a central element and is closely associated with the functions that use it, thus protecting it from accidental modifications by other functions.
Encapsulation provides an effective way to protect data from accidental damage. It is better to define them (fields) as private (privat) in many ways than we do with data (implemented with domains) defined as public in the program. Private data can be controlled indirectly in two ways. The first method, we use the traditional method of saving and fetching. The second method we use property.
The use of attributes can not only control the legality of access data, but also provide "read-write", "read-only", "write-only" flexible operation methods.
to enclose one or more items in a physical or logical package. can be referenced and invoked for the class library type.
Access modifiers:
Private: Only the class itself can be accessed.
Protected: Classes and derived classes can be accessed.
Internal: Only classes in the same project can be accessed.
Protected Internal: A combination of Protected and Internal.
Public: Full access.
Second, Inherit:
Inheritance mainly implements reusing code, saving development time.
1. Inheritance in C # conforms to the following rules:
- Inheritance is transitive. If c derives from B and B derives from a, then C inherits not only the members declared in B, but also the members of a. The object class acts as the base class for all classes.
- such as a,b,c three classes,a:b b:c The class a inherits all the members of the B and C classes. Left dial hand the right parent, the child inherits the parent.
- The derived class should be an extension to the base class. A derived class can add new members, but cannot remove the definition of a member that has already been inherited.
- Constructors and destructors cannot be inherited. Other members, regardless of the type of access they define, can be inherited. The way members in a base class can access only determines whether derived classes can access them.
- A derived class can overwrite an inherited member if it defines a new member with the same name as the inherited member. However, this is not because the derived class has deleted these members, but they are no longer accessible.
- Classes can define virtual grammars, virtual attributes, and virtual index indicators, and its derived classes can overload these members, enabling classes to demonstrate polymorphism.
2. New keyword
If a protected or public method is declared in the parent class that does not have a friend adornment, a method with the same name is also declared in the subclass. You can hide methods in the parent class with new. (Not recommended)
3. Base keyword
The base keyword is used to access the members of the base class from a derived class:
- Calls a method on the base class that has been overridden by another method.
- Specifies the base class constructor that should be called when creating an instance of a derived class.
Three, polymorphic
Different objects that are implemented by inheritance call the same method, showing different behavior, called polymorphism.
1, polymorphic: The same operation on different objects, can have different interpretations, resulting in different execution results. At run time, you can invoke methods in the derived class by pointing to a pointer to the base class.
Compile-time polymorphism:
The compile-time polymorphism is implemented by overloading. For non-virtual members, the system compiles, depending on the parameters passed, the type of return, and other information to decide what to do.
Run-time polymorphism:
Runtime polymorphism means that the operation is not determined according to the actual situation until the system is running. In C #, the runtime's polymorphism is implemented through virtual members.
Compile-time polymorphism provides a fast-running feature, while runtime polymorphism brings a highly flexible and abstract feature.
2. Realize polymorphism:
- Interface polymorphism.
- Inherit polymorphism.
- The polymorphism implemented by an abstract class.
3. Override Keyword:
Overrides the virtual decoration method in the parent class to implement polymorphism.
"Turn" C # three major features of encapsulation, inheritance, polymorphism