Composite, source file organization, category

Source: Internet
Author: User

Composite (composition): Connect multiple objects together to accomplish the desired functionality. In OC, a compound is implemented by including an object pointer that is an instance variable. @interface unicyle:nsobject{     pedal *pedal;     tire *tire;} The @end      //Unicyle  access (accessor) method is a method for accessing or altering an object's properties. If you want to manipulate the properties in other objects, you should try to use the access methods provided by the object, and never directly change the values inside the object. That's not safe, it's actually set and get.  -(void) Setfire: (tire*) Tire atindex: (int) index;/* The method name here is Setfire:atindex: The method is in order to have a better description of the parameters, such as C + + One of the methods in Isright (int. bool), Isright (false) when making a call, makes it difficult to tell what the specific meaning of each parameter is, so it is easier to understand that each parameter is described in the method in OC. The method here will be called when the [car setfire:tire atindex:i], so that the meaning of the parameter is better understood */  in OC all the interaction between the objects are implemented by pointers.   "Cannot find interface declaration for Tire" (Unable to find an interface definition for the Tire Class), usually because you forgot to import the header file of the class with #import.   Typically, each class has two files: the header file that contains the @interface part of the class and the M file that contains the @implementation section. The user of the class can use the #import command to import the header file to get the functionality of the class.   Use the @class directive to tell the compiler that this is a class.   Using OC's dynamic run-time allocation mechanism, you can add new methods to existing classes, called categories in OC. Programmers are always accustomed to placing category codes in separate files, typically named after the "class name + category name" style. You can add attributes to a category, but you cannot add an instance variable, and the property must be of type @dynamic. The advantage of adding properties is that the setter and getter methods can be accessed through point expressions. Category hasTwo limitations. The first is the inability to add new instance variables to the class. The category has no space to hold instance variables. The second is name collisions, and the methods in the category have the same names as existing methods. When a name conflict occurs, the category has a higher priority. The class method completely supersedes the initial method, causing the initial method to no longer be available. Add a prefix to your category method name to ensure that no name collisions occur.   in cocoa, categories have 3 main uses: Spread the implementation code of the class across multiple different files or frameworks, create forward references to private methods, and add informal (informal protocol) to objects. You can use categories if you want to spread large, single classes across multiple different. m files. Categories can also distribute methods into logical groups, making it easier for programmers to read header files.    

Composition, source file organization, category

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.