C # basic NOTE 2

Source: Internet
Author: User

Fields of the 23>> class

Once a variable is defined in a method, the inside of the class is called the field of the class. This variable can be accessed by all methods in this class, but it is important to note that static methods can only access static fields. (Constants modified by const cannot be defined as static.)

23>> Method Overloading

A method overload can be constituted in the same class, where the method name is the same, and the number of parameters of the method differs or the type at the corresponding location is different. (Note: Method overloads and return values do not matter)

1  Public Static intMax (intI1,inti2)2   {3      if(i1>i2)4        {5            returnI1;6         }7     Else8        {9            returnI2;Ten        } One    } A  Public Static intMax (DoubleI1,Doublei2) -   { -If (i1>i2) the          { -              returnI1; -           } -        Else +          { -              returnI2; +          } A}

24>> class

Properties: Objects have a variety of characteristics, each of which has a specific value for each property.

Class: A class is a mold that determines the characteristics (attributes) and behavior (methods) that an object will have.

Class is the type of object

An object can be called an instance of a class

The class does not occupy memory, and the object occupies memory.

To define the syntax for a class:

[Access modifier] class name

{

Members

}

A class can contain definitions and methods of variables. In our own class we write the method, we do not add static first.

Class is instantiated with the keyword new syntax: class instance name = new Class ();

Access to members of the class: Instance name. Property instance Name. method

Any methods and properties that are not static modified must be instantiated first, and accessed by the instance name. Method name or instance name. property. The static method or property can be accessed directly from the class name. method or class name. property name.

25>> Construction Method

Advantages of the construction method:

1. Duplicate write instance name is not required for multiple attribute assignments

2, you can ensure that the user in the new object must be assigned to a property value

3, and 2, when you create an object, the read-only property is initialized

When we have defined a class, if we do not write the construction method, then the system will give us a default no parameters of the construction method, in this method do nothing, we also see nothing

We can define a construction method:

The constructor method is a special method whose name is the same as the name of the class, and there is no return value, and even void can be omitted. Once we hand-write the method, the original system by default to us the addition of the non-parametric construction method will not be added for us.

Exception: To throw an exception, you can write:

throw new Exception ("Informational error");

26>> Property

What is called attribute: The popular point is to define a public variable that has the Get/set method, which is used to protect a private variable.

property is designed to protect the fields corresponding to the field, ensuring that the fields are read and assigned in accordance with the requirements.

Variables that allow external access must be declared as attributes.

Three attributes: Read-write, read-only, write-only properties.

1PrivateintAge ;2Publicint Age3 {4   Get5 {6   returnAge ;7 }8 Set9 { TenIf (value>0) One { AAge = value;//here is a keyword value to store user-assigned values - } - Else the { -Age =0; - } - } + } -Zsperson.age = -;//Call Set +Int AAA = Zsperson. Age;//Call Get

27>>get{return field;} set{field =value;}

is actually a property. property allows external programs to access your private fields (provided your property is public). However, the most important function of the property is that the field can be processed in the get and set, including judgment range and evaluation. This guarantees the correctness of the data and ensures that the values you read outside are correct and timely. Perhaps the most important function of a property is to do a read-only or write-only property, which is most useful. Control the reading and writing of the field to ensure the correctness of the program.

1 Private intSS;2  Public intSS3 {4     Get5     {6        returnSS;7     }8     Set9    {   Ten     if(Value >0) OneSS =value; A    }
}

This is a simple property. When setting the value, if you set the value to be less than 0, then the assignment will not be done. Only values greater than 0 are assigned to the SS. If it is a read-only attribute, it is only get, no set, and if it is a write-only property, only set, no get.

C # basic NOTE 2

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.