Hello everyone, I am a person
Wawawa ...
I'm 160 centimeters tall.
Wawawa ...
I'm 170 centimeters tall.
I don't want to die!
I don't want to die!
Please press any key to continue ...
The **************** code is as follows *******************
Class Renlei//Add a "human" class
{
public int Shengao;//Declare an integer variable of height
Public Renlei ()//This method is called a constructor (called a constructor with the same name as the method name and the class name)--the advantage is that the program will run once when it is loaded
{
Console.WriteLine ("The Crying Wawawa at birth...");
}
~renlei ()//This method is called the destructor (features: With the same name, preceded by a "~")-the advantage is: just put the man-made out to kill, good miserable endure ... Whining
{
Console.WriteLine ("I don't want to die!");
}
public void Shuohua ()//Declare a method of speaking at birth (of course, the child can not speak, only cry Wawawa...) )
{
Console.WriteLine ("I'm tall."+ Shengao +"centimeters");//This method does one thing: is to tell you, my height is how many miles meters
}
}
Class Shangdizaoren//Project namespace (just to tell you that the project name is:shangdizaoren)
{
static void Main (string[] args)//Program main entrance (this can not be confused ah, no main entrance, you can't even enter the door)
{
Console.WriteLine ("Hello everyone, I am a person");
Renlei Zhangsan;//give this person a name first
Zhangsan = new Renlei ();//Build a Zhang San first
Zhangsan.shengao = 160;//Use the newly created Zhang San to call the height variable Shengaoto get the height of the Zhang San, that is, to give him a value
Zhangsan.shuohua ();//Using the newly created Zhang San to invoke the method of Shuohua (), that is to print out
Renlei lisi=new Renlei ();//Recreate a John Doe
Lisi.shengao = 170;
Lisi.shuohua ();
}
}
The constructor creates the object, and the destructor destroys the object.
Destructors do not have parameters and cannot be called.
Analyze the Code
The constructor public Renlei () is automatically invoked at the time of creation, so Renlei zhangsan= i renlei () when he automatically cries Console.WriteLine ("Crying at birth Wawawa ...");
The destructor is automatically called when the program exits, so there is Console.WriteLine ("I Don't Want to die");
As for two times, because you call two times Renle () created 2 people, so to kill 2 people,
Deepened my understanding of the destructor
Constructors and destructors