Search C # (class one) a week
C # Talent Bird (QQ: 249178521)
1. Class statement
· Class is a user-defined reference type
Class Pair
{
Public int x, y; // public variable name of the first letter capital (PASCALCASE rules)
}
Class Pair
{
Private int x, y; // Non-public variable name first word first letter lowercase (Camelcase rules)
}
Class Pair
{
INT X, Y; / / The default access modifier is private
}; / / Can have end sections
You can use keyword class to declare this user-defined reference type. The syntax and structure of the class is very similar. It provides functions, fields, constructor, operators, and access control. The default access to class members is Private. When you define the membership of the class, don't forget to use the PASCALCASE rules for public members, and use the CamelCase rules for non-public members.
Although the end of the category can be used, it is recommended that you don't use it, this is just to take care of the C programmer's habits.
2. The result of the object
· A class of local variables exist in stacks (STACK)
W is not pre-assigned
W can be initialized to NULL or call constructor for initialization
(In the example below, please see the situation in front, the right side is displayed, @ represents pointing)
Stack of stacks
Static void
Main
()
{
PAIR P; P?
}
Static void
Main
()
{
PAIR P = NULL; P NULL
}
Static void
Main
()
{
PAIR P = new pair (); p @ 0 .x
} 0 .y
Although the statement of the statement and structure is very similar, the class and structure are two different types. The structure is a value type, and the class is a reference type. Regardless of the examples of the class, the local variables of the class are just a reference to this class instance.
The uppermost program in the above example defines a local variable P of a PAIR class. Regardless of the PAIR class contains any members, P just a reference existing in the stack. Because the P is not initialized, this reference is not assigned, and P is not available.
The intermediate program in the above example defines a local variable P of a PAIR class. P is initialized to NULL, so P does not point to any object. P has been assigned, so P can be used.
The lowermost program in the above example defines a local variable P of a PAIR class. This newly generated object is generated in a heap (HEAP), and P is defined in the stack in the heap, then Points to the object in this heap, and P is defined as in the stack. P has been assigned, so P can be used.
New is a bunch of objects for classes, and for structures that generate a value in the stack, which may take a period of time.
3. Structure of the object
· Constructor of class! = Constructor of the structure
w compiler declares the default constructor
w You can declare the default constructor
w If you declare constructor, the compiler does not declare constructor
Class Pair
{
}
// Compiler declares a default constructor
Class Pair
{
Public Pair ()
{...}
}
/ / Correct, you can declare the default constructor
Class pair {
Public Pair (int x, int y)
{...}
}
/ / Correct, but the default constructor declared by the compiler does not exist, there is no default constructor
The rules of the default constructor of the class are different from the rules of the default constructor of the structure. You can recall, the structure always has a compiler declared public default constructor. No matter if you have a declaration function, the compiler declared the public default constructor always exists. So you can't define the default constructor, which will have two default constructor, which is not allowed.
But this is only suitable for the structure, which is not applicable to the class. If you don't declare any constructor, the compiler produces a default constructor. But if you define a constructor, the compiler does not produce a default constructor. This also means that if you declare one or more constructor, you can have the only way you have the default constructor is that you must have a default constructor in the constructor you declare. This result is if you only define a non-default constructor, and you have to use the default constructor, then you only overload this constructor. If you must manually initialize every field in the class (just like in the structure), it will be very troublesome. But fortunately, you can't do this, you will see this below.
4.:this (...)
· A constructor can call another constructor
Sealed Class Pair
{
Public Pair (int x, int y)
: this (x, y, color.red)
{
}
Public Pair (int X, int y, color c)
{
...
}
...
Private int x, y;
Private colour C;
}