Constructors, method overloading, and attributes

Source: Internet
Author: User

Constructor is a special method of a class. In fact, it is used to initialize the class. The constructor has the same name as the class. There is no return value and no void is required. Each time you create an instance of the class, it is called at the time of new.

All classes have constructor methods. If you do not encode them, the system generates an empty constructor by default. If you have a defined constructor, the default constructor will become invalid.

Here, the so-called empty constructor does not actually do anything, just to allow you to instantiate it smoothly.

Cat cat=new Cat();

In the above Code, CAT () after new is actually the constructor. Here is an empty constructor, just to allow the program to be instantiated smoothly. Let's look at the following sentence:

Cat cat = new CAT ("kitten ");

CAT ("kitten") is a constructor. When the class is instantiated, that is, when the kitten was born, we named it "kitten ".

To achieve the above purpose, when a kitten is born, it will initialize a "kitten name" for it. We need to add a constructor and reload a method in the class.

Private string name = ""; Public CAT (string name) {This. Name = Name;} public CAT () {This. Name = "kitten ";}

Through method overloading, we have added new functions based on the original functions.
Next, let's talk about attributes.
Attribute is a method or a pair of methods, but in the code that calls it, it is a field, that is, the attribute is suitable for the use of method calls in the form of fields.

The property has two methods: Get and set. The get accessor returns the same data type as the declared attribute. Indicates that the value or reference of an internal field can be obtained during the call;

The Set accessors do not explicitly set parameters, but they have an implicit parameter, which is expressed by the keyword value. Its function is to assign values to internal fields or references when calling properties.

The usage is as follows:

private int ShoutNum=1;public int ShoutNum  {      get {return shoutNum;}      set {shoutNum=value;}  }

The code above indicates that we can directly read the value 1 of the shoutnum variable, and we can also re-assign a new value to it. The new value will overwrite the original initial value.

 

 

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.