I. Overview of the Class
Class is the template that creates the object, each containing data, and provides a way to process and access the data. In other words, classes define each object, which is what data and functionality the "instance" contains.
For example, we define a "Doctor" class and instantiate one. Let's look at the following code:
using System;
namespace gosoa.com.cn
{
public class doctor
{
Public doctor () {}
Public Doc Tor (String Name,byte age)
{
this._name=name;
This._age=age;
}
private string _name;
Private byte _age;
public string Name
{
Get{return this._name;}
Set{this._name=value;}
}
Public byte of age
{
Get{return this._age;}
Set{this._age=value;}
}
public String dosth ()
{
Return "I will treat a person well ~ ~";
}
public static string Doanth ()
{Another static method executed by
return ";
}
}
public class Onedoctor
{
static void Main ()
{
Doctor dc=new doctor ();
DC. Name= "Dick";
DC. age=25;
Doctor dc2=new Doctor ("John", 35);
Console.WriteLine (DC. Name);
Console.WriteLine (DC. Age);
Console.WriteLine (DC2. Name);
Console.WriteLine (DC2. Age);
Console.WriteLine (Dc.dosth ());
Console.WriteLine (Doctor.doanth ());
}
}
}
In this example, the public class doctor declares a class. _name and _age are its two properties. Dosth () is one of its methods (that is, the behavior of the object). The Doctor dc=new Doctor () is used to instantiate a doctor class, and a similar instantiation of an object, resulting in a new doctor. Doctor Dc2=new doctor ("John", 35); it's another class that's instantiated, another doctor.
In the Doctor class, the two methods, public doctor () {} public doctor (String Name,byte age), are called constructors. is used to initialize the class, and is invoked automatically when each class is instantiated.
public string Name
{
get{return this._name;}
set{this._name=value;}
}
This code is used to set and get the properties of the class. It is similar to the GetName and SetName methods in Java. It's just getting easier in C #.
Note that a class is a reference type that is stored on the managed heap.