Basic concepts:
1. Classes are encapsulations of business processing objects, including state and behavior.
2. Types of membership of class:
1. Constant: a symbol where the data value is constant
2. Field: Field represents a read-only or read-write data value. Fields are usually used to identify a certain state of a class or class that produces an object.
In practice, fields are usually identified as private, avoiding destroying the state of the class/object from outside the class/object.
3. Instance constructor: A special method to initialize the instance fields of a new object into a good initial state.
4. Type constructor, that is, static constructor, is used to initialize static fields of the class.
5. Method: A special function used to change or query the state of a type or object.
6. Properties: Used to encapsulate external access to fields and avoid direct access to fields.
7. Event: Encapsulate a delegate chain and notify the method in the delegate chain to execute through a certain triggering mechanism.
8. Subtype: A type nested in a type is to separate complexity.
9. Operator overloading: Redefine specific operations between objects generated by the class: for example +, -, ++, etc.
10. Conversion operator: Defines how to display or implicitly convert a class to another type.
Class modified keywords:
1. Accessibility modification:
1. public: used for non-necked classes, with unlimited access.
: Used for non-necked classes, visible only to all code that defines the assembly.
: Used for nesting classes, visible only to members of the class that contains it.
: Used for nested classes, visible only to the members of the class A containing it and the derived class A_Derived of A.
2. Static class modifiers
: Used to define classes that never need to be instantiated, such as Console, Math, Environment, etc.
2. Static classes cannot inherit base classes other than those, nor can they implement interfaces
3. Static classes can only define static members
4. Static classes cannot be used as fields, method parameters, or local variables, because they all represent variables that reference an instance.
3. Divisional class modifiers
: Used to decompose a class or structure into different logical units, with the purpose of decomposing the complexity of the type.
2. For common scenarios, when Winform defines a form or control, the front-end unit and logical unit are automatically generated.
IV. Components and polymorphic modifiers
: Indicates that an abstract class cannot directly generate an instance of the class, and can only be implemented through inheritance.
: Indicates that this type cannot be used for base types.
: A nested type used to define in a derived class, indicating that the nested type is independent of the nested type of the same name in the base class.
Modification keywords for class members
1. Accessibility: Not explained in detail, it is similar to class modification
2. Static members: no detailed explanation
3. Components and polymorphic modifications:
: Used as a base class member, indicating that in order to construct an instance of a derived type, the derived type must be implemented and overridden.
: Used as a base class member, indicating that this member can be rewritten by the derived type
: Used to derived class members, indicating that the derived type overrides the member of the base type.
: Used as a base class member, indicating that this member cannot be rewritten by the derived type and can only be used in methods.
: Used to derive a class member, indicating that this member does not have an overload relationship with a member of the same name in the base class.
Knowledge best practices in this chapter
I. Best practices for designing accessibility of classes and members:
1. When defining a class, unless it is determined that it will be used as the base class, it should be specified as Sealed.
The reason is that the behavior of the derived type is unpredictable. If the base class is not sealed, the derived class defined in subsequent versions or other team members may destroy the state or expected behavior of the base class.
2. When defining a class, if you are not sure that this class will be published outside the assembly, you should specify the class as internal.
The reason is also due to safety considerations.
3. Inside the class, all data fields should be defined as private because of protection of state. The state of each class/object should only be controlled by itself.
4. Inside the class, unless a method is determined and the attribute needs to be overridden in the subclass, do not use virtual.
There are two reasons. One is that calling virtual methods will consume more performance in the CLR; the other is that virtual methods will lose control of the base class's own behavior.
5. When defining nested classes in a class, the nested classes should be modified as private. This rule will be checked for force in VS.
2. Similarities and differences between static classes and singleton patterns:
1. Similarities: Both provide a single entry for type members
2. Differences:
a. Static classes are difficult to control initialization timing
b. Static classes do not support base classes, derived classes, and implement interfaces, and have poor support for polymorphisms.
c. The implementation of static classes is relatively simple, and the security is higher without supporting polymorphism.
1. Classes are encapsulations of business processing objects, including state and behavior.
2. Types of membership of class:
1. Constant: a symbol where the data value is constant
2. Field: Field represents a read-only or read-write data value. Fields are usually used to identify a certain state of a class or class that produces an object.
In practice, fields are usually identified as private, avoiding destroying the state of the class/object from outside the class/object.
3. Instance constructor: A special method to initialize the instance fields of a new object into a good initial state.
4. Type constructor, that is, static constructor, is used to initialize static fields of the class.
5. Method: A special function used to change or query the state of a type or object.
6. Properties: Used to encapsulate external access to fields and avoid direct access to fields.
7. Event: Encapsulate a delegate chain and notify the method in the delegate chain to execute through a certain triggering mechanism.
8. Subtype: A type nested in a type is to separate complexity.
9. Operator overloading: Redefine specific operations between objects generated by the class: for example +, -, ++, etc.
10. Conversion operator: Defines how to display or implicitly convert a class to another type.
Class modified keywords:
1. Accessibility modification:
1. public: used for non-necked classes, with unlimited access.
: Used for non-necked classes, visible only to all code that defines the assembly.
: Used for nesting classes, visible only to members of the class that contains it.
: Used for nested classes, visible only to the members of the class A containing it and the derived class A_Derived of A.
2. Static class modifiers
: Used to define classes that never need to be instantiated, such as Console, Math, Environment, etc.
2. Static classes cannot inherit base classes other than those, nor can they implement interfaces
3. Static classes can only define static members
4. Static classes cannot be used as fields, method parameters, or local variables, because they all represent variables that reference an instance.
3. Divisional class modifiers
: Used to decompose a class or structure into different logical units, with the purpose of decomposing the complexity of the type.
2. For common scenarios, when Winform defines a form or control, the front-end unit and logical unit are automatically generated.
IV. Components and polymorphic modifiers
: Indicates that an abstract class cannot directly generate an instance of the class, and can only be implemented through inheritance.
: Indicates that this type cannot be used for base types.
: A nested type used to define in a derived class, indicating that the nested type is independent of the nested type of the same name in the base class.
Modification keywords for class members
1. Accessibility: Not explained in detail, it is similar to class modification
2. Static members: no detailed explanation
3. Components and polymorphic modifications:
: Used as a base class member, indicating that in order to construct an instance of a derived type, the derived type must be implemented and overridden.
: Used as a base class member, indicating that this member can be rewritten by the derived type
: Used to derived class members, indicating that the derived type overrides the member of the base type.
: Used as a base class member, indicating that this member cannot be rewritten by the derived type and can only be used in methods.
: Used to derive a class member, indicating that this member does not have an overload relationship with a member of the same name in the base class.
Knowledge best practices in this chapter
I. Best practices for designing accessibility of classes and members:
1. When defining a class, unless it is determined that it will be used as the base class, it should be specified as Sealed.
The reason is that the behavior of the derived type is unpredictable. If the base class is not sealed, the derived class defined in subsequent versions or other team members may destroy the state or expected behavior of the base class.
2. When defining a class, if you are not sure that this class will be published outside the assembly, you should specify the class as internal.
The reason is also due to safety considerations.
3. Inside the class, all data fields should be defined as private because of protection of state. The state of each class/object should only be controlled by itself.
4. Inside the class, unless a method is determined and the attribute needs to be overridden in the subclass, do not use virtual.
There are two reasons. One is that calling virtual methods will consume more performance in the CLR; the other is that virtual methods will lose control of the base class's own behavior.
5. When defining nested classes in a class, the nested classes should be modified as private. This rule will be checked for force in VS.
2. Similarities and differences between static classes and singleton patterns:
1. Similarities: Both provide a single entry for type members
2. Differences:
a. Static classes are difficult to control initialization timing
b. Static classes do not support base classes, derived classes, and implement interfaces, and have poor support for polymorphisms.
c. The implementation of static classes is relatively simple, and the security is higher without supporting polymorphism.