O-C related 04: Overview and Definition and invocation of class methods

Source: Internet
Author: User

Overview and definition and invocation of class methods

1, Overview of class methods

Class methods are often referred to as static methods (such as Java or C #, etc.) in other programming languages. Unlike an instance method, a class method can be called only by using the class name , without referencing the object, and without having to create the object. The instance method must first create the object (instance) and then use the object to invoke the method.

Use "+" in OC to denote class methods, and "-" for instance methods.

A comparison of the definition of a class method with an instance method:





2, definition of a class method




3, advantages of using the class method:

-A class method is in the code area, so you don't need to create an object

Frequent use of class methods to improve efficiency

Class method always consumes memory

4. Considerations for Class methods

1) class method and instance method allow duplicate names

2) class method can inherit from parent class, subclass allows overriding

3) class methods, like instance methods, are declared in the @interface and implemented in the @implementation

4) The class method only allows messages to be sent to the class to allow calls

5) The self in the class method is no longer an instance object, but the class object of the Class (class)


5, errors that are easy to make with class methods

1) A member variable is used in a class method

2) An instance method is called in the class method


6. Comparing class methods to instance methods, a class method can be used if a method does not need to use a member variable, from a comparison between a class method and an instance method. Or the class method is generally used as a tool method.





Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

O-C related 04: Overview and Definition and invocation of class methods

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.