封装
#import
@interface Student : NSObject
{
}
set方法
- (void)setAge:(int)newAge;
- (int)age;
- (void)study;
@end
@implementation Student
set方法的实现
- (void)setAge:(int)newAge
{
}
- (int)age
{
}
- (void)study
{
}
@end
int main()
{
}
==================================================
封装细节
#import
typedef enum {
} Sex;
@interface Student : NSObject
{
}
- (void)setSex:(Sex)sex;
- (Sex)sex;
- (void)setNo:(int)no;
- (int)no;
@end
@implementation Student
- (void)setSex:(Sex)sex
{
}
- (Sex)sex
{
}
- (void)setNo:(int)no
{
}
- (int)no
{
}
@end
int main()
{
}
==================================================
类方法
#import
类方法的好处和使用场合
@interface Person : NSObject
{
}
// 类方法都是以+开头
+ (void)printClassName;
- (void)test;
+ (void)test;
@end
@implementation Person
+ (void)printClassName
{
}
- (void)test
{
}
+ (void)test
{
//
}
@end
int main()
{
}
==================================================
self
#import
@interface Person : NSObject
{
}
- (void)setAge:(int)age;
- (int)age;
- (void)test;
@end
@implementation Person
- (void)setAge:(int)age
{
}
- (int)age
{
}
- (void)test
{
}
@end
int main()
{
}
==================================================
self
@interface Dog : NSObject
- (void)bark;
- (void)run;
@end
@implementation Dog
- (void)bark
{
}
- (void)run
{
}
@end
int main()
{
}
==================================================
self
工具类:基本没有任何成员变量,里面的方法基本都是类方法
@interface JiSusnQi : NSObject
+ (int)sumOfNum1:(int)num1 andNum2:(int)num2;
+ (int)averageOfNum1:(int)num1 andNum2:(int)num2;
@end
@implementation JiSusnQi
+ (int)sumOfNum1:(int)num1 andNum2:(int)num2
{
}
+ (int)averageOfNum1:(int)num1 andNum2:(int)num2
{
}
@end
int main()
{
}
==================================================
self使用注意
@interface Person : NSObject
- (void)test;
+ (void)test;
- (void)test1;
+ (void)test2;
- (void)haha1;
+ (void)haha2;
@end
@implementation Person
- (void)test
{
}
+ (void)test
{
}
- (void)test1
{
[self test];
}
+ (void)test2
{
}
- (void)haha1
{
}
void haha3()
{
}
+ (void)haha2
{
}
@end
int main()
{
}