Classification/protocol/attribute

Source: Internet
Author: User
1. classification: when we want to add some methods to a class, if we don't want to inherit the class, we can add some behavior to the class by classification. This process is lighter than inheritance.
@ Interface nsstring (subclass)-(ID) dosomething (); @ end @ implementation nsstring (subclass)-(ID) dosomething () {// do something...} @ end

In the code above, the first line, subclass is the category name, And nsstring is the class for adding behavior. The implementation of classification is similar to that of general classes.
2. Protocol: the protocol is a set of predefined behavior methods, which is similar to interfaces in Java. The protocol can be divided into formal and informal protocols. The former is the method that must be defined by the class using the protocol, and the latter is optional.
@ Protocol handleevent {-(void) handlekeyupevent;-(void) handlekeydownevent: (ID) sender ;}
3. attribute: the attribute is used to access the instance variables of the object. The usage is to use @ property in the header file (. h) to declare an attribute, and then use @ synthesize in the implementation file (. m) to tell the compiler to automatically generate an access method for a variable.
@ Property (nonatomic, retain) nsstring * Name; @ synthesize name;

 

Classification/protocol/attribute

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.