Public; private; static; Private Static; public static

Source: Internet
Author: User
Since there is no chance to learn to write large projects, there is little detailed analysis Public; Private; Static; Private Static; Public Static. Today, my head suddenly becomes blank. After two hours of searching for information, I got the following content and posted it specially. I hope I can help some young friends. You are also welcome to point out my mistakes or omissions.

Public: Public. Function: Used to declare public variables or methods. That is, it can be referenced outside the class that declares the variable or method.

PRIVATE: private. It is used to declare internal private variables or methods. That is, it cannot be referenced outside the class that declares the variable or method.

Static: Static. It is used to declare the variables or methods directly called using this class. Directly use this keyword (addPublic(Note: static variables can only be obtained through classes in C .), Variables or methods can be referenced externally.

The following is an example:

PublicClass gettime
{
PublicString time ()
{
Return System. datetime. Now. tolongdatestring ();
}
Private string time1 ()
{
Return System. datetime. Now. tolongdatestring ();
}
PublicStaticString time2 ()
{
Return System. datetime. Now. tolongdatestring ();
}
}

All the above three methods return the current time. Let's take a look at the differences between the three methods. In C #, The New Keyword is widely used, assume that this class is referenced in another class.
PublicClass tsting
{
Gettime n_gettime = new gettime ();
String time = n_gettime.time (); // here, you can access the gettime method, which isPublicThe other two classes cannot be accessed. To access time2., you only need to write samples.
Sting time2 = gettime. time2 ();
// Time1 () declared with the private keyword cannot be accessed externally .}

UseStaticThere is also an essential difference from not to use, that is, when a variable is declared, if its own value is 0, after the value is one hundred, ifStaticIf the keyword is not used, it is 0.

(If any expert finds an error, please help !)

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.