Object-Oriented Programming note--static

Source: Internet
Author: User

Provides static methods or properties that you do not need to instantiate to access by means of the static method. All callers can use the same class (not instantiated) or an object (instantiated only once) that can be applied to the scene:

1) Each caller shares data and works together.

2) An object can only be instantiated once.

3) The life cycle of the called object is independent of the caller. Or, the object has a global life cycle, which continues to work, providing only one interface to the caller.

An instance of a class is maintained by the class itself and provides a way to take an instance.

Typical code:

 Public classtestclass{//Instances of Maintenance    Private StaticTestClass instance; //static methods for externally available get instances     Public StaticTestClass Instance {Get        {            if(Instance = =NULL) Instance=NewTestClass (); returninstance; }    }}

Application Examples:

The definition of DependencyProperty in WPF

/** Two ways to use * 1) use dictionary in the form of a total class to record all properties registered by the static registration method of the class * 2) to generate a new object **/  Public classdependencyproperty{Internal Staticdictionary<Object, dependencyproperty> Registereddps =Newdictionary<Object, dependencyproperty>(); Internal stringName; Internal ObjectValue; Internal Objecthashcode; //constructor Function    PrivateDependencyProperty (stringName, type propertyname, type OwnerType,ObjectDefaultValue) {         This. Name =name;  This. Value =DefaultValue;  This. Hashcode = name. GetHashCode () ^Ownertype.gethashcode (); }    //Registration Function     Public StaticDependencyProperty Register (stringName, type PropertyType, type ownertype,ObjectDefaultValue) {DependencyProperty DP=NewDependencyProperty (name, PropertyType, OwnerType, DefaultValue); Registereddps.add (DP.        Hashcode, DP); returnDP; }}

Object-Oriented Programming note--static

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.