Class is the abstraction that describes the common features of a group of similar objects. In general, things are similar. For example, there are a bunch of piglets in the pigsty. We give each of them a name, such as Xiaohua, Xiaoxiao, xiao, and xiaoxxx .... There are four or two small flowers, one for small white and two for small black. So the question is, what are the common characteristics of Xiaohua, Xiaoxiao, and xiaohei? OK. Our answer is that they are all pig! Why don't you say they're wolves? Cubs? With the concept of IS-A, small flower is a piglet, small white is a piglet, small black is also a piglet, in line with the small flower is (is) (-A) a piglet, this IS-A said. The opposite is wrong. We cannot say that pig is a small flower. So in this example, Small and Medium pig are the type, while small flowers, small white, and small black are the type? It is an object, a class object, or an instance of a class. They share the common characteristics of the class, and fat animals with four legs-pig. In other words, the class is like a pastry mold in a pastry factory. The moon cakes from the mold are exactly the same in appearance. Of course, they may have different contents. But each pig has different names given to it based on their skin color and class points, and their weights are also different. So what can a pig do? In short, we can eat and sleep, so what can we do is an action, usually a class method.
Keep in mind: class is used to represent concepts. Find out the commonalities and differences of things. The differences are established on the existing commonalities. The commonalities are often classes (interfaces or abstract classes ).
Therefore, the class C # is used to write this instance:
Class littlepig // pig
{
Private double height; // weight
Private string name; // name
Public littlepig (string name, double height) // Constructor
{
This. Name = Name;
This. Height = height;
}
Public double height // attribute of height
{
Get {return height ;}
Set {This. Height = value ;}
}
Public void eat () // how to eat
{
Console. writeline ("{0} is eatting.", name );
}
Public void sleep () // sleep method
{
Console. writeline ("{0} is sleepping.", name );
}
}
Class Test
{
Static void main ()
{
// Create a Class Object (or instance)
Littlepig Xiaohua = new littlepig ("Xiaohua", 0.4 );
Littlepig Xiaobai = new littlepig ("", 1.0 );
Littlepig xiaohei = new littlepig ("", 2.0 );
Xiaohua. Eat (); // call the method. When you call the method, you will see a drop-down list with the two methods we have written. Pay attention to the small icon on the left of the method. You can also see the defined height attribute in the drop-down list. Pay attention to the small icon on the left.
}
}
The height and name of the member variables in the class are attributes. You do not need to worry about them. The attribute concept in C # refers to the getter (read method) and setter (write method) of the class member variables) the purpose of a collection is to provide read and write control for private variables of the class. Therefore, member variables are called class attributes, which have more extensive meanings or attributes in a broad sense.
I don't know if my explanation is clear enough? If you can understand what the event is and what the delegate is? Rome was not built in one day.