Object-c Object-oriented
Class: Collectively, a group of objects with the same characteristics (the concept of a group of similar objects)
Class: A collective name for a group of objects.
Object: something that exists.
To define the syntax for a class:
OBJECT-C defines a class that requires 2 parts:
The part of the header file (declaration part)-the equivalent dial portion, exposed for user operation.
@interface < class name >: NSObject
{
member variables
}
Method declaration
@end
class name: As long as it is an identifier. However, from the point of view of program readability, the class name must be concatenating by one or more meaningful words.
member Variable: The state information that is used to describe the object of the class or class. The program only records status information related to the business.
Syntax:< type > < variable name >;
method: Used to describe the behavior of the class or the object of the class. The program only records business-related behaviors.
method Syntax:
+|-(return value type) method Name: (parameter type 1) parameter name 1 parameter label 2: (parameter type 2) parameter name 2 parameter label 3: (parameter type 3) parameter name 3 ...
+ indicates that the method belongs to the class itself and that the method can only be called by the class-indicating that the method belongs to the object of the class and therefore can only be called by the object.
Returns a value type that can be any valid type.
The method name, as long as it is a valid identifier. But from the point of view of program readability, the method name should be one or more words concatenating, the best method name is a verb.
formal parameter list: In addition to the first parameter, each of the following parameters requires 3 parts: Parameter label: (parameter type) parameter name
The implementation portion-equivalent to the body part of the table-is completely hidden, but is responsible for providing the display of the dial portion.
@implementation < class name >
{
the member variable--the member variable defined here, is the equivalent of a member variable that is hidden.
}
Implement all the methods defined in the interface section.
Additional ways to define the implementation section. --the additional method defined here is equivalent to being hidden and therefore cannot be called.
@end
The role of the class:
1. Define variables. All classes belong to the pointer type.
2. Create the object.
3. Call the class method.
4. For inherited, derived subclasses.
"Nonexistent": the class variable can be accessed ———— but does not exist. The Object-c class cannot define a class variable.
Creating objects
[[Class name alloc] init]
Init, including the following series of Initwithxxx methods, is called the initialization method.
[class name new]-rarely used.
The purpose of the object:
1. Invoking an instance method
2. Accessing instance variables
object variables, member variables
Calling methods
[Class | Object method: Parameter 1 formal parameter label 2: Parameter 2 formal parameter label 3: Parameter 3 ... ];
Objects and pointers
When declaring a variable with a custom class, you need to add a * after the class name to indicate that it is a pointer.
Pointer: means that when a program assigns an object to a pointer variable, it actually simply stores the object's first address in a pointer variable, not actually storing the object in the pointer.
The same object can be pointed to (referenced) by a number of pointer variables (reference variables).
In turn: For a pointer variable, you can point at most one object.
Object--only assignment, no object is created. The object is created to see allocate (allocated memory).
dealloc--deletes the memory and destroys the object.
The self keyword represents the caller of the calling method. In layman's terms, who invokes the method, self represents who.
If it is always called by an instance in an instance method, self always represents the object that called the method;
If it is always called by a class in a class method, self always represents the class itself that invokes the method.
Self, mainly as a pronoun. When it appears in any method, who calls the method, self represents who.
ID Type
C/object-c are strongly typed languages, and variables of the specified type can only accept objects of the specified type.
But unlike IDs, it can accept objects of any type.
For a variable of type ID, it can dynamically parse and bind the method to be invoked at run time.
It needs to be explained that the dynamic binding must be bound to the actual method--or it will compile an error.
Dynamic binding is only performed by methods, and member variables do not perform dynamic binding even if the member variable is an ID type.
Note: If the program assigns a value to a member variable of a pointer variable of type ID, the program compiles an error. The method is detailed:
Understanding Method:
1. The nature of the method and function is the same.
2. When defining a method, the method cannot exist independently and must be defined within the class. Some methods (+) belong to classes, and some (-) are instances (objects)
3. When invoking a method, the method must also have the caller-the caller is called the keynote (subject, caller)
For the + method, the caller must be the class itself. For the-method, the caller must be an instance.
Pig Eat watermelon
[Pig Eat: watermelon];
Pig. Eat (watermelon);
A method for changing the number of formal parameters
When you call the method, you can pass in multiple values for the parameter.
When declaring a method, you need to use the,... form to declare a variable number of parameters.
When implementing a method, you need to use the following to get the multiple arguments passed in when the method is called:
Va_list: This is equivalent to a pointer type.
Va_start: It is a function. Va_start (va_list type variable, variable number of formal parameter names);
Va_arg: It is also a function that gets a parameter value each time.
Va_end: After processing is complete, use this function to turn off variables of type va_list.
#import "FKComputer.h" @implementation fkcomputer -(void) Print: (nsstring*) content, ... {//define a variable of type va_list to get the variable number of arguments passed in by the program Va_list arglist;//if the content exists-at least the first value of the content parameter is present. if (content) {//The first value of the content parameter is processed first NSLog (@ "%@", content);//Bind the parameter list of the content parameter to Arglistva_start (argList, content);// Each time the program calls Va_arg, a parameter value can be obtained. nsstring* arg = Va_arg (argList, id);//If ARG is not nil, the parameter is processed--but if nil was last taken, the loop ends. while (ARG) {NSLog (@ "%@", arg), arg = Va_arg (argList, id);} Va_end (argList); End processing. } } @end
Attention:
1. A variable number of parameters can only be used as the last parameter of a method, so a method can declare at most one variable number of parameters.
2. When invoking a method with a variable number of parameters, the program must pass at least one parameter to a variable number of formal arguments.
Variable
Global variables: Declared within the scope of the source file (no longer any curly braces).
stored directly in the static store, the global variable will not die when the program exits.
The system automatically assigns the initial value to the global variable. The initial value is 0 generalized.
A local variable: declared in a function or method.
Saves the current method or function stack, and therefore dies with the end of the method or function.
Local variables The value of a local variable is indeterminate before the program explicitly assigns an initial value.
Member variable (instance variable): In a pair of curly braces after the class (interface or implementation section).
The member variable allocates memory as the object allocate, and the member variable begins to appear.
Member variables are destroyed as the object is recycled.
Instance variable, which is born as the instance is born, and dies as the instance dies.
--in the ARC mechanism, if an object does not have any reference variables pointing to it, then the object is destroyed.
The system automatically assigns the initial value to the member variable. 0 (0, 0.0, nil) with a generalized initial value
Essentially, ALLOC is responsible for allocating memory when the object is created, and Init is responsible for performing the default initialization.
Life time:
Local Variables < member variables < global variables
Object-c All member variables are instance variables, so OC has only instance variables and no class variables.
Simulating class variables
A static global variable can be used to simulate a class variable.
Static global variable-the global variable that can be accessed only in the current source file, internal global variables.
Steps:
(1) Define a static global variable in the implementation section of the class.
(2) A class method for declaring and implementing a set of getter and setter for this class, and for manipulating the static global variable
Where getter methods are used to get the values of global variables; Setter methods are used to modify the values of global variables.
Packaging
Object-oriented 3 major features: encapsulation, inheritance, polymorphism.
Encapsulation: everywhere. The package includes 2 meanings:
-Hides the implementation details of the object inside. Reasonable concealment.
-Exposes the interface for user operation. Reasonable exposure.
Object-c How to implement encapsulation:
By accessing the control character.
@package
@private @public
@protected
@private: A completely hidden access control character. A member variable that is controlled by the @private and can only be accessed in the current class. (Completely hidden)
@package: A member variable controlled by @package that can be accessed only in the same image of the current class and the current class. (Partially hidden: not hidden within the same image)
@protected: A member variable that is controlled by @protected and is accessible only in all subclasses of the current class and the current class. (partial exposure)
@public: A member variable controlled by @public that can be accessed from anywhere. (Completely exposed)
The current class has the same image as the current class. Full range of subclasses of the current class
@private √xxx
@package √√xx
@protected √x√x
@public √√√√
If the member variable does not use an access control character, the @protected adornment is used by default.
The OBJECT-C permission control, which starts with the current permission control, is managed to the next permission control.
In general, when designing a class, the recommended practice is to:
(1) Hide the member variable with private to avoid direct access to the member variable by the program.
(2) Provide setter, getter method for this member variable, setter, getter method can add control access logic,
This avoids the damage caused by directly modifying the value of the member variable of the object.
For methods, you cannot use @private, @protected, @package, @public control.
All methods that are declared in the interface section are exposed methods (equivalent to public).
A method that is only implemented partially, not declared in the interface section, is a private method (equivalent to private).
About @package's understanding
1. Members that are @package, can certainly be accessed in the current class.
2. Same image. As long as the current class is compiled into a single file (including the same executable file, the same library file, etc.), it can be called the same image.
Later, when you develop your app, you can simply declare a member as @package.
Synthetic getter, setter method
(1) Declare the attribute with @property.
@property (designator) nsstring* brand;
A property definition, you can get 3 things:
(1) Get a member variable that adds _ prefix in front of the property name-this is a member variable and is completely hidden.
(2) Getter method.
(3) Setter method.
If we think that the system implements the getter, setter method is not good, the program can also be implemented in the implementation of part of the setter, setter method.
(2) optional. If you want to change the name of the member variable that corresponds to the @property composition attribute, you need to use the @synthesize directive in the implementation section.
@synthesize attribute name; --The name of the member variable that corresponds to the composition attribute is exactly the same as the property name.
@synthesize Property name = new name; --the member variable that corresponds to the composition attribute is named the new name.
OBJECT-C Foundation (7)--Classes and objects