<>C# Study notes (4) class

Object oriented programming is mainly to write a general code , Shielding difference , Facilitate the development of large projects . Class is one of the core of object-oriented programming . and C Compared with the structure of language , Functions of structures , There are all kinds , besides , Class also has many new features .

<> Class composition
[ Other modifiers ] [ Access modifier ] class Class name { field ; attribute ; method ; Constructor ; Destructor ; }
<> field

Store private data of class , Usually, it can only be accessed by the properties of the class .

<> attribute

Protection field , Limit the assignment and value of the field . For example, there is a student class , Class has this field , You can only get grades outside the class – excellent good average poor , At this point, you can add an attribute to this field , Access the score field indirectly by accessing the attribute . To ensure program encapsulation , Fields outside the class are generally not allowed to access fields inside the class , Generally, fields and attributes appear in pairs .
public class Student { int _Score; // Score field public string Score // Achievement attribute { get { if
(_Score < 60) { return " difference "; } else if(_Score < 80) { return " in "; } else if(
_Score< 90) { return " good "; } else { return " excellent "; } } set { _Score = int.Parse(
value); } } }
The essence of attributes is two methods , One is called get() One is called set().
Existing get() Also set() We call it readable and writable .
only get() No, set() We call it a read-only attribute
No, get() only set() We call this write only attribute

<> method

The method is C Functions in language , Object oriented is called method .

* C In language , Function name cannot be duplicate , and C# Duplicate method names are allowed in ( Method overloading ), However, the number of method parameters is required to be different , Or different types . public class Student
{ int _Score; // Score field public string Score // Achievement attribute { get { if(_Score < 60) { return
" difference "; } else if(_Score < 80) { return " in "; } else if(_Score < 90) { return " good "; }
else { return " excellent "; } } set { _Score = int.Parse(value); } } // All three methods are called Learn
method 1,2 The number of parameters is the same, but the types are different method 1,3 The parameter types are the same but the number is different public void Learn(string subject, int time)
{ Console.WriteLine(" study {0} subject , Learning duration :{1}", subject, time); } public void Learn(
string subject1, string subject2) { Console.WriteLine(" study {0} subject , study {1} subject ",
subject1, subject2); } public void Learn(string subject, int time, float ratio)
{ Console.WriteLine(" study {0} subject , Learning duration :{1}, learning efficiency :{2}", subject, time, ratio); } }
<> Constructor

effect : Help us initialize objects ( Assign values to the properties of the object )
Constructor is a special method :
1), Constructor has no return value , even void You can't write .
2), The constructor name must be the same as the class name .

* The constructor is executed when the object is created
* Constructors can be overloaded .
Class will have a default parameterless constructor , When you write a new constructor , Whether it's parametric or
Parameterless , The default parameterless constructor was killed .
public class Student { int _Score; // Score field public string Score // Achievement attribute { get { if
(_Score < 60) { return " difference "; } else if(_Score < 80) { return " in "; } else if(
_Score< 90) { return " good "; } else { return " excellent "; } } set { _Score = int.Parse(
value); } } // Default constructor You can also add parameters and method bodies yourself public Student() { } }
<> Destructor

effect : Destructor (destructor)
Contrary to constructor , When an object is out of its scope ( For example, the function where the object is located has been called ), The system automatically executes the destructor . Destructors are often used to do “ Clean up the aftermath ”
Work of ( For example, when creating objects new Opened up a piece of memory space , Should be used in the destructor before exiting delete release ).

*
Different from constructor . It cannot take any parameters , There is no return value ( include void type ). There can only be one destructor , Cannot overload . If the user does not write a destructor , The compilation system will automatically generate a default destructor , It also does nothing .
Destructors are a special method :
1), Destructors cannot be inherited or overloaded .
2), Destructors cannot be called by users .
3), Destructors cannot have modifiers or arguments .
4), If there is no destructor, the system will automatically generate an empty destructor , The resources used by the object will have GC Garbage
Collection Garbage collector collection ( If users want to release resources themselves , You have to do it yourself Writing destructors ) public class Student { int _Score;
// Score field public string Score // Achievement attribute { get { if(_Score < 60) { return " difference "; } else
if(_Score < 80) { return " in "; } else if(_Score < 90) { return " good "; } else {
return " excellent "; } } set { _Score = int.Parse(value); } } // Default constructor You can also add parameters and method bodies yourself
public Student() { } // Default destructor You can also add your own method body ~Student() { } }
<> Modifier

<> Access modifier

* public : Public
* private: Private , Can only be accessed inside the current class
* protected: Protected , Can only be accessed inside the current class and in subclasses of that class .
* internal: Can only be accessed in the current project . In the same project ,internal and public The permissions are the same .
* protected internal:protected+internal
1), There are only two access modifiers that can modify a class :public,internal.
2), In inheritance , When accessibility is inconsistent ,
The access permission of the subclass cannot be higher than that of the parent class , Otherwise, members of the parent class will be exposed .

<> Other modifiers

*
partial Partial class

In cooperative development , A class may be written by more than one person , Everyone writes in different files , You can use partial To modify the class , Indicates that the current is only a part of this class .

*
sealed Sealing class

DINK , Cannot be inherited

*
static

Difference between static and non static
1), In a non static class , You can have both instance members , You can also have static members .
2), When calling instance members , Object name is required . Instance member ;
When calling static members , Class name is required . Static member name ;
summary : Static members must be called with the class name , Instance members use object names to call .
In static function , Only static members can be accessed , Access to instance members is not allowed .
In instance function , You can use static members , You can also use instance members .
Only static members are allowed in static classes , Instance members are not allowed .

use :
1), If you want your class to be a " Tool class " To use , At this time, you can consider writing the class as static .
2), Static classes share resources throughout the project .
Only after the program is all over , Static classes release resources .

Technology
©2019-2020 Toolsou All rights reserved,
Android Using wechat in H5 Payment result refresh during payment shock !!C++ Can make a sound ! Basic operation of single linked list C Language explanation Java Implement an epidemic number management system 2021 year 11 World programming language ranking linux upper mysql Invalid command _linux lower mysql The command is useless Java project : Campus dormitory management system (java+jsp+javaweb+mysql+ajax) Wechat applet development project directory linux ubuntu Which version ,Ubuntu Which version is the best ?python Code painting cherry blossoms - How to use Python Draw a beautiful cherry blossom