I. Category
1. What is a class?
A class is a general term for things with the same features. Therefore, a class is an abstraction, that is, it is not an entity (we regard the class as a template ).
2. What is an object?
An object is an entity created based on a class template. It has all the features of the class. There is no more or no more. If it is less, it will not be called a member of this class. If it is more, it will not be! Assume that Michael has the function of transforming to another person.
Remember that an object is created based on a template. If a template has any content, there will be no more or fewer objects!
3. What is a field (or a member variable )?
We call the variables defined outside the method (namely, the class) in the class as fields or member variables.
4. Class Definition: (create using the class keyword)
Class Name
{
Class member;
}
Note: classes are abstract and cannot be used directly. We can only use them after creating class objects. (Understanding: for example, we build a house drawing. You can't tell your wife that I bought a house. What about your house? Then you give her a piece of paper, and you want ....)
5. Create a Class Object
Syntax: Class Name object name = New Class Name ();
Note: The New Keyword has done four things in the above sentence:
1> first, the New Keyword opens up a suitable size space in the heap space.
2> Create class objects according to the class template.
3> call Constructor
4> return the memory address of the object in the heap space.
6. Members of the category class:
Syntax: Object Name. member. Use the preceding syntax to define the attribute members and methods of the category class.
7. Data Type and this keyword of the class
A. Based on the syntax and principle of the created object (that is, the structure in the memory), we can know that the class is a reference type.
B. This keyword indicates the current instance (or the current object ).
Ii. namespace
1. What is the role of the namespace?
The namespace is mainly used to solve the problem of duplicate class names.
2. quickly introduce the namespace method: Right-click the class name (ensure it is correct) and choose> resolve.
3. If multiple classes are in the same namespace, we do not need to introduce the namespace using. We directly create an object instance to call class members.
4. We can repair the default namespace name. Do not think that you do not need to use using in the same folder. It mainly depends on whether the namespaces are the same. If they are the same, they are not required. If they are different, you need to import the namespace.
5. When using other classes in the code, you need to introduce the class namespace using before using it.
3. Access Modifier
1. access modifier for Class Members
1> Public: public is the highest permission that can be accessed anywhere.
2> private, which can only be accessed in this class.
3> internal can only be accessed in the current program. Cross-assembly access is not allowed.
4> protected is protected and can only be accessed in this class and subclass. It cannot be accessed except its own class and subclass.
5> note: the default access level for class members is private.
Ii. Class access modifier
1> the public-modified class can be accessed anywhere, but you still need to follow the instructions: Introduce the namespace-> Create the Class Object-> Members of the classes Class.
2> internal-modified classes can only be accessed in the current Assembly.
3> Note: if the class does not have a write access modifier, the default value is internal. Remember that the class does not have a private or protected modifier (understanding: the private-modified member can only be used internally by the current class. If the modified class is used, it is accessed internally by the class?
Can be meaningful? If the class you write does not want to be accessed and you only want to use it by yourself? What is the significance of writing a class? Class is to encapsulate things with the same features for reuse), if the code is executed only once in the entire program, it can be completely written in the main method ).
Iv. Attributes
I. Syntax:
[Access modifier] attribute data type attribute name
{
Get
{
Return value;
}
Set
{
Field name = value;
}
}
Abbreviation: [access modifier] attribute data type attribute name {Get; set ;}
Ii. Attribute assignment and Value
1> you can use the keyword value in the set method to obtain the user-assigned value.
2> the value is returned through return in the get method.
3> attributes are essentially two methods.
4> attribute values cannot be saved. Values exist in fields. If the abbreviated vs compiler is used, it will automatically generate two private fields for us. In essence, we still operate on fields.
5. Object initializer
I. Syntax:
For example: person P = new person () {name = "Tom", age = 18}
1> assign values to each attribute of the created object in braces. Each attribute is separated by commas;
2> when the object initializer is used to assign values to an object, if no parameter constructor is called, the following "()" can be omitted.
Example: person P = new person {name = "Tom", age = 18}
3> the premise that "()" can be omitted is as follows:
A. You must assign values to attributes using the object initializer.
B. It must be a non-parameter constructor called. If the constructor has a parameter, no error is returned.
4> the object initializer can assign values to all objects or assign only partial values without doing anything.
Example: person P = new person {}; (valid)
6. Constructor
I. Syntax:
[Access modifier] Class Name (parameter)
{
Function body;
}
1> the access modifier is generally public.
2> the constructor does not return values, and does not even use void.
3> the constructor can be overloaded (with parameters can be included), and the parameter can be transferred when the new object is used.
4> if no constructor is defined, the system will have a default constructor (No parameter). If no constructor is defined (No parameter is required) the system will not define the default constructor for us.
5> the constructor is called and executed by the New Keyword when the object is created. The constructor cannot be manually called by the programmer. A constructor can call a common method. A common method cannot call a constructor (it can only be called by new when an object is created ).
6> constructor features: the code in the constructor is automatically executed when an object is created.
7> constructor: generally used to assign values to fields or attributes of an object. The assignment process is completed in the constructor even if the field contains an initial value! If no initial value is available, assign the default value to the field when executing the constructor,
Value Type = 0, reference type = NULL. That is, constructors are used to initialize objects.
8> constructor can call each other.
Syntax: A constructor (): This (parameter) // B Constructor
{
Method body;
}
The compiler will match the called constructor according to ": This (parameter.
Execution Process:
1> call the constructor first.
2> call the B constructor.
3> execute the B constructor first.
4> execute the constructor.
Note: When passing parameters to the B (called constructor) constructor, either the constant value or the parameters of the (constructor) constructor cannot be given! A constructor can call only one Constructor (two or more constructor cannot be called ).
VII. Classification
1. In the same namespace, multiple classes with the same class name cannot be defined, because this will cause a conflict. However, if multiple classes with the same name are defined in the same namespace but they are all modified by partial, this is acceptable. Because they are essentially the same class (Department class or partner class ).
Because the C # compiler will compile them into a class, you can use members of another class in one class.
Ii. Partial classification conditions:
1> In the same namespace.
2> the class names are the same.
3> classes are all modified by partial
Iii. Function of partial classification: easy to manage. For example, to define a human, this class may have 10 thousand fields. We can put them in a subclass to facilitate management and maintenance.