Each instance variable defines two methods: Set the setter method of the variable to get the getter method of the variable value
Writing specification for Set method
The Set method must be an object method
The Set method must not have a return value
The Set method must start with set
Set followed by the instance variable minus the underscore, and the first letter capitalized
The set method must have parameters, and the type of the parameter is consistent with the type of the instance variable
The formal parameter name of the set method is usually to remove the instance variable name of the next loop
Parameter name cannot have the same name as instance variable name
In the implementation of the Set method, it is important to assign values to the instance variables using formal parameters
Write specification for Get methods
The Get method must be an object method
The Get method must have a return value, the type of the return value and the type of the instance variable to be consistent
The method name of the Get method is to remove the instance variable name from the next loop
The Get method must have no parameters
In the implementation of the Get method, the value of the instance variable must be returned
The following shows the code for the set and get methods
We create a person class in the. h file with the following code:
#import <foundation/foundation.h> @interface person:nsobject{nsstring *_ Name int _age;} // -(void ) SetName: (nsstring *) name; -(NSString *) name; // -(void ) Setage: (int -(int @end
The code in the. m file is as follows:
#import "Person.h"@implementation Person//The setter method of member variable _name and the implementation of Getter method- (void) SetName: (NSString *) name{_name=name; }-(NSString *) name{return_name;}//The setter method of member variable _age and the implementation of Getter method- (void) Setage: (int) age{//in this can limit the age of entry is legal if(Age >= -) {_age=Age ; } Else{_age= -; } }- (int) age{return_age;}@end
There are no overloads of methods in OC, so-called parameters one is because the name of the method has changed, hint: do: ::
Use of the static keyword in OC: The only thing to note is that member variables cannot be defined as static, other times use the same as in C, see my previous blog >---point I can see---<
The self keyword in oc, Self is used to represent this class in a class method, and self is used to represent this object in an object method
Self modifier variable: Access the member variable see Code: (Still based on the person class) in the. m file
// The setter method and the getter method implementation of the member variable _name -(void) SetName: (NSString *) name{ Self->speed = Speed ; // self->speed equivalent to _name// _name = name; }-(NSString *) name{ return _name;}
Here again, the four characteristics of the face object are
1. Abstraction 2. Encapsulation 3. Inheritance 4. Polymorphism
Abstraction: Abstraction is about ignoring aspects of a topic that are not related to the current goal, so that you can more fully focus on the aspects that are relevant to the current goal. Abstractions do not intend to understand all of the problems, but simply select one part of them, temporarily without some detail. For example, we want to design a student performance management system, to examine the student this object, we only care about his class, school number, grades, etc., and do not care about his height, weight of this information. Abstract includes two aspects, one is the process abstraction, the other is the data abstraction. Process abstraction refers to the fact that any operation that explicitly defines a function can be viewed by the user as a single entity, although the operation may actually be done by a series of lower-level operations. Data abstractions define the data types and actions that are applied to objects of that type, and limit the values of the objects to be modified and observed only by using these operations.
There are about encapsulation, inheritance, polymorphism please see my previous blog (also introduced in the category) >---Click here---<
Introduction to member Variable modifiers
- @public public, can be accessed from anywhere through instance objects
@private private, which means that only the current class can be used in a subclass that cannot be used but inherited by the quilt class.
@protected protected type, indicating access only in the current class and subclass (default is protected)
Description method: Overriding the description method of the parent class can change the output of the NSLog
If you use NSLog to print self in the-description method, it will cause a dead loop
The code is as follows
// overrides the parent class's description-(NSString *) description{//do something .... // For example return [NSString stringwithformat:<# (NSString *), ...#>];}
The nature of the class is the object
Class object belongs to class type
// 2 ways to get class objects class // class Method // or New class// Object Methods
A member variable cannot be called in a class method
SEL
1. Where to store the method
- The list of methods for each class is stored in the class object
- Each method has an object of the corresponding SEL type
- The address of the method can be found based on a Sel object, and the method is called
- Definition of SEL type
struct objc_selector *sel;
Creation of 2.SEL objects
SEL s == nsselectorfromstring (@ "test");
Other uses of 3.SEL objects
// Convert Sel object to NSString object NSString *str =new]; // call the test method of the object P [P Performselector: @selector (test)];
The point syntax in OC is the feature of Xcode, and Xcode does a code replacement for us.
// Xcode will help us replace the following code with [person setage:18]; - ; // Xcode will help us replace the following code with [person age]; int a = person.age;
@property and @synthesize
@property compiler directives, the compiler helps us to _age _name Get/set method declaration
@synthesize help us implement the get and set methods of instance variables
Xcode4.4 after the @property can help me to _age _name Get/set method declaration, can also implement the instance variable get and set method
int*name;
ID is a universal pointer that can point to any object
The construction method in OC
Overriding the construction method so that the object is created successfully, there is an initial value
oc is: Init object method The method returns an object (the object that called the Init method)
// - Span style= "color: #000000;" > (instancetype) init{ // Let the parent do what the parent does originally do self = [super Init]; // Determine if the parent class is initialized successfully if // This writes the contents of the subclass initialization _age = 18 ; // Set the age to the default value of 18 years old return self; // self means the caller of the method
Custom Construction Methods
The. h file code is as follows
#import <Foundation/Foundation.h>@interface person:nsobject@property (nonatomic, copy) NSString*int age ; // Custom Construction Method -(Instancetype) Initwithname: (NSString *) name andage: (int) age; @end
The. m file code is as follows
#import " Person.h " @implementation Person-(Instancetype) Initwithname: (NSString *) name andage: (int) age{ if ( Self = [Super init]) { = name; = Age ; } return Self ;} @end
Objective-c Knowledge Summary (2)