Home > Article > Backend Development > C# Supplementary Knowledge (3): Class
is an abstract concept.
For example, the Dog class describes some characteristics of a dog, such as weight, height, age, roar, etc.
public class Dog { string dogBreed;//犬种 int weight; //体重 int height; //升高 int age; //年龄 public void bellow() { Console.WriteLine("旺旺"); } }
This Dog class is abstract and describes some characteristics, not a specific dog.
Let’s define a neighbor’s dog "Daha", which belongs to the Dog class.
Dog Daha = new Dog();
Instantiate the object that creates "Daha". Now "Daha" is a concrete existence. After that, you can give a detailed description of the "Daha" object. For example, the dog breed of "Daha" is Husky, weighing 35 kilograms, etc.
Daha.dogBreed = "Husky";
Daha.weight = 35;
……
After giving way, "Daha" roars
Daha.bellow(); This is a dog’s roaring method
Note: Properties cannot be directly assigned to the Dog class. Just like int = 8;, it is meaningless. You cannot say that dogs have the same weight, height, or breed without referring to a specific dog.
The biggest benefit of a class is that it can encapsulate the properties and behavior of an entity in an independent unit of code. According to the above example, the Dog class encapsulates the dog breed, height, weight, age attributes, and the roaring method.
Access types include Public, Private, Protected (accessible by inheritance), internal, and Protected internal (accessible by inheritance). Available for classes, fields, methods, properties, and constructors.
Classes can contain: fields, properties, constructors, and methods.
Class member method:
Syntax:
Access type return type method name (accepts parameters,)
{Method body}
Access type defaults to public
For example:
Public void Method(int i , string s,…….) {方法体}
Multiple methods: The method name is the same, but the parameters are different
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 creating an object. The function name of the constructor is the same as the class name. 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;//犬种 int weight; //体重
}
The above example contains two multiple constructors that accept different parameters.
This represents the current instance (referencing the current object), the dogBreed field of the Dog instance.
Class member variables (fields):
In the above example, dogBreed and weight are member variables of this class. It can be initialized at the time of definition instead of initializing each variable in the constructor.
Class member attributes:
C# provides the get;set; accessor. Use attributes to encapsulate class data.
Example:
private int height; public int Height { get { return weight; } 读取 set { weight = value; } 赋给 }
In the above example, other classes cannot read height directly and can only access it through the accessor.
The access type can be set before get set.
Object destruction:
Destructor, destructor declaration in C#:
~textClass() { 析构处理 }
It can also be written as:
textClass.Finalize() { 析构处理 Base.Finalize(); }
The above is the content of C# Supplementary Knowledge (3): Class. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!