Small knowledge (3): class and small knowledge
Reference page:
Http://www.yuanjiaocheng.net/entity/tixijiegou.html
Http://www.yuanjiaocheng.net/entity/setenvrionment.html
Http://www.yuanjiaocheng.net/entity/createdatamodel.html
Http://www.yuanjiaocheng.net/Spring/first.html
Http://www.yuanjiaocheng.net/entity/modelbrowser.html
Class is an abstract concept.
For example, the Dog class describes some characteristics of a Dog, such as weight, height, age, and roar.
Public class Dog
{
String dogBreed; // dog breed
Int weight; // weight
Int height; // raise
Int age; // age
Public void bellow ()
{
Console. WriteLine ("trademanager ");
}
}
This Dog class is abstract and describes some features, not a specific Dog.
Let the following define a neighbor's Dog "Daha", which belongs to the Dog class.
Dog big Haha = new Dog ();
Instantiate and create a "Daha" object. Now "Daha" exists. Then, you can describe the "Big ha" Object in detail. For example, the dog type of "da ha" is husky and its weight is 35 kg.
Daha. dogBreed = "husky ";
Big ha. weight = 35;
......
Let the "Big haha" Scream
Big ha. bellow (); this is a dog's way of shouting
Note: The Dog class cannot be assigned a value directly. For example, int = 8; is meaningless. You cannot say that the weight, height, and species of a dog are the same.
The biggest benefit of a class is that it can encapsulate the attributes and behaviors of an object in an independent code unit. According to the example above, the Dog class encapsulates the Dog type, height, weight, age attributes, and the ROAR method.
Access types include Public, Private, Protected (inherited and accessible), internal, and Protected internal Protected (inherited and accessible ). Applicable to classes, fields, methods, attributes, and constructors.
A class can contain fields, attributes, constructors, and methods.
Class member method:
Syntax:
Access type return type method name (accept parameter ,)
{Method body}
The default access type is public.
For example:
Public void Method (int I, string s ,.......)
{Method body}
Multiple methods: the same method name and different parameters
Public string d (int I, int j)
{}
Public void d (string I)
{}
Public void d (int I)
{}
Public int d (char d)
{
Return 0;
}
Class constructor:
New initializes member variables when an object is created. The name of the constructor is the same as that of the class. There can be multiple constructors, just like multiple methods.
Example:
Class Dog
{
Public Dog (string dogBreed, int Weight)
{
Weight = Weight;
This. dogBreed = dogBreed;
}
Public Dog (string dogBreed)
{
This. dogBreed = dogBreed;
}
String dogBreed; // dog breed
Int weight; // weight
}
The preceding example contains two constructors that accept different parameters.
This indicates the dogBreed field of the Dog instance of the current instance (referencing the current object.
Class member variables (fields ):
In the above example, dogBreed and weight are member variables of this class. Initialization can be performed during definition, instead of initializing every variable in the constructor.
Class member attributes:
C # provides get; set; accessors. Encapsulate class data with properties.
Example:
Private int height;
Public int Height
{
Get {return weight;} Read
Set {weight = value;} is assigned
}
In the above example, other classes cannot directly read the height, and they can only be accessed through the accessors.
You can set the access type before the get set operation.
Object destruction:
Destructor, The Destructor declaration in C:
~ TextClass ()
{
Structure Processing
}
You can also write it as follows:
TextClass. Finalize ()
{
Structure Processing
Base. Finalize ();
}
These are my learning documents. If there is any error in this article, please point it out. Thank you!
References: Typical C # programming practices. Edited by Tsinghua University Press and FU Qiang Ding Ning. Chapter 3.