In this section we will talk about an overview of classes
. We have always said that C# is object-oriented programming, and we learned earlier that each object must belong to a class. Many classes have been defined in .net, but in actual programming , we must also customize some classes for the program.
All classes in C# inherit directly or indirectly from the System.Object class, which allows classes in C# to inherit from a single root. If we do not explicitly specify an inherited class, the compiler defaults to thinking that the class inherits from the System.Object class. The System.Object class can also be represented by the lowercase object keyword. The two are completely equivalent.
To be precise, there are three main components of the class, and of course there are others. We will not go into details here. I will give an in-depth tutorial later when I have learned more. Let’s talk about it later!
1. Attributes: Attributes store object information. As we said earlier, accessor methods are used, so they can be read-only or readable and writable.
2. Methods: Methods allow you to Using objects to perform an operation can actually interact with other objects through property settings, but methods can complete some composite operations.
3. Events: We just talked about events in the last class. They can do corresponding things when triggered.
Finally, when we have completely defined a class, we can encapsulate it. When using it, we don't care how it completes the task. We just need to know what public interfaces it has, what properties it has, and what methods and events it has. These are collectively called class members.
As we said before, we can use classes to create N objects, but they are all isolated. So is there any way we can share objects between two specific classes?
Let’s get to the point. We can define a series of methods or attributes and apply them to the class as a whole instead of being specifically used on objects of a certain class. They are what we call shared members (static members). For example, if we want to know how many books have been published, we use the shared attribute count attribute, or to adjust the prices of all books, we use the shared method modemon(), which can give a 20% discount to all books.
The class contains some variables. The data of these variables is included in the member variables (the private variables we often mentioned earlier). It also contains some operation codes, and these codes are included in the member functions. We also operate the data through the member functions. In practice, In the operation, we only provide its properties, methods, events and other interfaces, and the data part is all hidden, or encapsulated.
Member variables and member functions are collectively called class members. They are divided into static and entity members.
Let’s look at an example below
public static string aaa; --------static member variable
public static void aaa() --------static member function
{
}
public string aaa; -------------Entity member variable
public void aaa() -------------Entity member function
{
}
Notice that the static keyword is used
in the above. When using static member variables, you can directly use the
entity member without declaring the object. You must first declare the object entity before using it.
To sum up, all objects in C# will be created in on the managed heap. The instantiated type is called an object, and its core feature is that it has its own copy of the data members. These data members held by specific objects are called instance members. On the contrary, those data members that are not held by specific objects are called static members and are declared with the static modifier in the class. Those that only perform operations on static data members are called static function members.
There is also void: what is returned after a general method or function is executed can be of any type, such as int or string. If you want to write a method that does not return a value, you must use the keyword void instead of the return type.
So we must have noticed the word public above. In fact, it has three brothers: privateprotectedinternal. They are called access modifiers, which are used to control external operations on class members to achieve the purpose of hiding data.
Below we will explain each one
public: Any external class can access class data without restrictions.
private: Limit the internal use of this class
protected: In addition to the class itself, any class that inherits this class can use
internal: It means that it can be used in the same application (Application) or class library (Library), but this is what we Basically no need.
http://www.cnblogs.com/thcjp/archive/2006/09/15/505022.html