Objective-c class, instance member, static variable, object method, class method (static method), object,
|
In iOS, the declaration of a class is separated from its implementation, that is, it cannot be written in the same file, the declaration is placed in the. h file, and the implementation is placed in the. m file. Introduce. h files in the implementation file, #import "Xxx.h"
Declare a class:
#import <Foundation/Foundation.h>
@interface Person:nsobject
@end
To implement a class:
#import "Person.h"
@implementation person
@end
In the iOS class the bar variable is called the instance variable, and the default permission is protected, in the class can only declare the instance variable, must be able to declare the method. And you cannot declare static instance variables in. h files, which can only be declared and used in. M.
Eg:
#import <Foundation/Foundation.h>
@interface person:nsobject{
int age;
nsstring* name; A string in iOS
static int dwint; Error, can ' t
}
@end
Third, static member variables
|
Static instance variables cannot be declared in the. h file, and can only be declared and used in. M.
Eg:
#import "Person.h"
@implementation person
static int dwint=20;
@end
Object methods cannot be declared in parentheses, can only be declared outside of parentheses, and precede with-.
#import <Foundation/Foundation.h>
@interface person:nsobject{
int age;
nsstring* name; A string in iOS
}
-(int) getage;
-(nsstring*) getName;
-(void) Setage: (int) _age;
-(void) SetName: (nsstring*) _name;
-(void) Setage: (int) _age andname: (nsstring*) _name;
@end
Implement. M
#import "Person.h"
@implementation person
static int dwint=20;
-(int) getage{
return age;
}
-(nsstring*) getname{
return name;
}
-(void) Setage: (int) _age{
Age=_age;
}
-(void) SetName: (nsstring*) _name{
Name=_name;
}
-(void) Setage: (int) _age andname: (nsstring*) _name{
Age=_age;
Name=_name;
}
+ (int) getstatic{
return dwint;
}
@end
Class methods cannot be declared in parentheses, can only be declared outside of parentheses, and precede with +.
#import <Foundation/Foundation.h>
@interface person:nsobject{
int age;
nsstring* name; A string in iOS
}
-(int) getage;
-(nsstring*) getName;
-(void) Setage: (int) _age;
-(void) SetName: (nsstring*) _name;
-(void) Setage: (int) _age andname: (nsstring*) _name;
+ (int) getstatic;
@end
Implement. M
#import "Person.h"
@implementation person
static int dwint=20;
-(int) getage{
return age;
}
-(nsstring*) getname{
return name;
}
-(void) Setage: (int) _age{
Age=_age;
}
-(void) SetName: (nsstring*) _name{
Name=_name;
}