2. Class
Grammar:
[Public] class name
{
Field
Properties
Method
}
After writing a class, we need to create the object of this class,
So, the process of creating an object for this class is called instantiation of the class. Use the keyword new.
This: Represents the object of the current class. The class is not in memory, and the object is memory-occupied.
3. Properties
The function of a property is to protect a field, assign a field, and qualify a value.
The nature of the property is two methods, one called Get () and one called set ().
Both get () and set () are the readable writable properties.
Only get () does not have set () What we call a read-only property
No get () only set () What we call a write-only property
1 Private string_name;2 3 Public stringName4 {5 //when you output a property value, the Get method is executed6 Get{return_name;}7 //When you assign a value to a property, the first thing you do is set method8 Set{_name =value;}9 }Ten //Set Method One Public voidSet_name (stringvalue) A { - This. _name =value; - } the //Get Method - Public stringget_name () - { - return This. _name; +}
View Code
Several words to keep in mind in C #: Field fields, Method methods, property properties
4. Access modifiers
Public: Publicly available, accessible anywhere.
Private: It can only be accessed within the current class, and this class is inaccessible.
5.
Once we have created an object of a class, we need to assign a value to each property of the object.
We call this process the initialization of the object.
6. Static and non-static differences
1), in a non-static class, there can be either an instance member or a static member. 2), when invoking instance members, you need to use the object name. Instance members;
When you call a static member, you need to use the class name. static member name;
Summary: Static members must be called using the class name, and instance members are invoked using the object name. In a static function, only static members can be accessed and instance members are not allowed.
Instance functions, you can use either static members or instance members. Only static members are allowed in static classes, and instance members are not allowed.
Use:
1), if you want your class to be used as a "tool class", consider writing the class as static.
2), static classes in the entire project resource sharing. A static class frees resources only after the program has all finished.
7. Constructor function
Role: Helps us initialize the object ( assigning values to each property of the object in turn )
A constructor is a special method:
1), the constructor does not return a value, and even void can not be written. 2), the name of the constructor must be the same as the class name.
Constructors are executed when the object is created, and constructors can be overloaded.
***
There is a default parameterless constructor in the class, and when you write a new constructor, the default parameterless constructor is eliminated, whether it is a parameter or no argument.
8. New keyword
Person Zsperson=new person ();
New has helped us do 3 things:
1), open a space in memory. 2), create objects in the open space. 3), invoking the object's constructor to initialize the object
9. This keyword
1), which represents the object of the current class.
2), the constructor for calling this class, shown in the class: this
1 Public classStudent2 {3 PublicStudent (stringNameintAgeCharGenderintChinese,intMathint中文版)4 {5 This. Name =name;6 This. Age =Age ;7 This. Gender =gender;8 This. Chinese =Chinese;9 This. Math =Math;Ten This. 中文版 =中文版; One } A PublicStudent (stringNameintChinese,intMathint中文版): This(Name,0,'male', Chinese,math,english) - { - //This . name = name; the //This . Chinese = Chinese; - //This . math = math; - //This . 中文版 = 中文版; - } + Private string_name; - + Public stringName A { at Get{return_name;} - Set{_name =value;} - } - Private int_age; - - Public int Age in { - Get{return_age;} to Set + { - if(Value <0|| Value > -) the { *Value =0; $ }Panax Notoginseng_age =value; - } the } + Private Char_gender; A the Public CharGender + { - Get $ { $ if(_gender! ='male'&& _gender! ='female') - { - return_gender ='male'; the } - return_gender;Wuyi } the Set{_gender =value;} - } Wu Private int_chinese; - About Public intChinese $ { - Get{return_chinese;} - Set{_chinese =value;} - } A Private int_math; + the Public intMath - { $ Get{return_math;} the Set{_math =value;} the } the Private int_english; the - Public int中文版 in { the Get{return_english;} the Set{_english =value;} About } the}
View Code
10, destructor
When the program finishes, the destructor is executed to help us release the resources
~studengt ()
{
Console.WriteLine ("I was used to release resources");
}
C # Fourth Day