Constructors and destructors

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.