1.声明类<声明类的名称参数方法>
@interface People : NSObject
{
@public
@private
@protected
}
- ( void ) Say;
+ ( void ) SayStatic;
@end
2.实现类<实现类的方法>
#import "People.h"
@implementation People
- ( void ) Say
{
NSLog ( @"Hello World !" ) ;
}
+ ( void ) SayStatic
{
NSLog ( @"This is a Static!" ) ;
}
@end
3.实例化类
#import "People.h"
int main ( int argc, const char * argv[ ] )
{
@ autoreleasepool
{
People * myPeople = [ [ People alloc] init] ;
}
return 0 ;
}
4. 对象方法&类方法
#import "People.h"
int main ( int argc, const char * argv[ ] )
{
@ autoreleasepool
{
People * myPeople = [ [ People alloc] init] ;
[ myPeople Say] ;
[ People SayStatic] ;
}
return 0 ;
}
5. 对象的初始化方法
声明类:
@interface People : NSObject
{
}
- ( instancetype) init;
@end
实现类:
#import "People.h"
@implementation People
- ( instancetype) init
{
self = [ super init] ;
if ( self ) {
NSLog ( @"初始化方法被调用!" ) ;
}
return self ;
}
@end
main方法:
#import "People.h"
int main ( int argc, const char * argv[ ] )
{
@ autoreleasepool
{
People * myPeople = [ [ People alloc] init] ;
}
return 0 ;
}
6. 属性&变量
属性可以被 对象 “点” 属性名 调用 变量可以被 对象 "->"变量名 调用
<声明变量&使用变量>
@interface People : NSObject
{
@public
int myInt;
@private
@protected
}
@end
#import "People.h"
int main ( int argc, const char * argv[ ] )
{
@ autoreleasepool
{
People * myPeople = [ [ People alloc] init] ;
myPeople-> myInt = 10 ;
NSLog ( @"myInt = %d" , myPeople-> myInt) ;
}
return 0 ;
}
<声明属性&使用属性>
@interface People : NSObject
@property int myInt;
@end
#import "People.h"
int main ( int argc, const char * argv[ ] )
{
@ autoreleasepool
{
People * myPeople = [ [ People alloc] init] ;
myPeople. myInt = 100 ;
NSLog ( @"myInt = %d" , myPeople. myInt) ;
}
return 0 ;
}
2019 - 06 - 01 RyccccCode