OC Basic Notes-class methods and object methods and encapsulation (Guangzhou Black horse Phase I)

Source: Internet
Author: User

Object method:
1. When declaring and implementing the method, start with-
2. Object methods are subordinate to objects and can only be called by objects
3. The member variable can be accessed directly in the object method because both the object method and the member variable are subordinate to the object's
4. Other object methods can be called in the object method
5. Class methods can also be called in Object methods
Class method:
1, Declaration and implementation of identifiers are +
2, class method It belongs to the class itself
3, the class method is called directly through the class name, cannot be called through the object
4. A class method cannot access a member variable because the member variable is an object, not a class
5. The object method cannot be called directly in the class method, that is, the object method cannot be called by self.
If you want to call the object method in the class method again
1. Create an object in the class method
2. Pass the object as a formal parameter

class Method Advantages:
1, call the class method without creating objects, so save memory
2, through the object Call object method, first of all through the object to find his Isa pointer, and then in the class to find the appropriate method, all relatively slow
3, directly through the class call method, directly find the class and then call the corresponding method is OK, all class method efficiency is higher
4. Use the class method when you do not need to use the member variable in the method

Packaging:
1, the class is the data and the function encapsulation, the data is the member variable, the function is the class method or the object method
2. Encapsulation of data
If we set the property to @public then this property can be arbitrarily modified externally, and we lose the right to manage this property.
1, when you expose the properties of the external part of the time, then you are losing management of it, once this attribute is used by multiple people, if again like the management of this property becomes very troublesome, this class of maintainability becomes very poor

2. How to encapsulate the data
1. Provide getter and setter methods for each attribute
2. Setter method: Also known as Write method, assign value to object assignment
1. Must be an object method
2. It has no return value
3. Name set + attribute name remove underline, capitalize first letter
4. A parameter must be supplied, and the parameter type must match the type of the member variable of the object
3, Getter Method: Also known as the Read method, the value of the Read property assignment value
1. Must be an object method
2, the value must be returned, and the return value type will be the same as the member variable type
3. The method name must be a member variable to remove the underscore
4, must be the null parameter

1, NSString
1. Creation of NSString objects
1, through the literal way
NSString *str = @ "literal";
2, through the object method
NSString *str = [NSString alloc];
str = [str initwithutf8string: "C-language string"];
Can merge a sentence
NSString *str = [[NSString alloc] initwithutf8string: "C language String"]
3. Create by class method
NSString *str = [nsstring stringwithformat:@ "formatted string",....];
Format string: Its format character is the same as in NSLog
2. Calculate the character length
[Str length];//counts the number of characters in a string, one Chinese occupies one character
2, BOOL Type: It is a logical type, logic type only true and False, YES true no false
1. Defining variables
2. Form parameter types for functions or methods
3, as a function or method of the return value type
Nature of the bool type: Char
YES 1
NO 0
3. Objects and functions
0, Functions: C language Functions, they are parallel, no one who belongs to the relationship, call directly through the function name, can be in the file anywhere
1. The object is passed as a function parameter, and if the property in the object is altered by a formal parameter in the function, it affects the properties of the object in the argument.
2. Parameter objects can also call their own functions when they can access their own members of the function
3. Object can return value of function
4. The object can be passed continuously in the function
4. Objects and methods
0: Method in OC, when defining the class declaration and implementation method, which is subordinate to an object or a class, the call must be called by the object or class;
1, the object as a method parameter delivery is the address delivery
2, objects can also access their own member variables in the method, you can also call their own functions
3, the object can also be the return value of the method
4, the object can be passed continuously in the method

5. Multi-File development
1, in order to facilitate the management of the program, we have a class divided into two files
. h file: A declaration used to hold a class, which is given to the person using the class, and the statement is equivalent to the dial
. m file: The method used to implement the class, the internal implementation of the table, such as gears
2, when we need to use a class, we need to import the header file of this class, cannot import the implementation file
3, when a two class directly appears in the loop contains, one end must use @class to declare this class
4, @class just tell the compiler that this is a class, other information about the class itself is not known, this @class is generally used only in the header file

OC Basic Notes-class methods and object methods and encapsulation (Guangzhou Black horse Phase I)

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.