封装:隐藏内部实现,稳定外部接口.
类封装了实例变量和方法
#import
{
NSString *_name;
NSString *_sex;
int _age;
@property(nonatomic,retain)NSString *name,*sex;
@property(nonatomic,assign)int age;
- (id)initWithName:(NSString *)name sex:(NSString *)sex age:(int)age;
-(Person *)work;
#import "Person.h"
@synthesize name = _name,sex = _sex,age = _age;
- (id)initWithName:(NSString *)name sex:(NSString *)sex age:(int)age
{
self = [super init];
if (self) {
self.name = name;
self.sex = sex;
self.age = age;
}
return self;
}
- (Person *)work
{
NSLog(@"%@正在工作",self.name);
return 0;
}
3、多态:不同对象对同一消息的不同响应.子类可以重写父类的方法
多态就是允许方法重名 参数或返回值可以是父类型传入或返回
#import "AppDelegate.h"
#import "Worker.h"
#import "Actor.h"
#import "King.h"
worker.name = @"工人";
[worker work];//worker 可以换为父类型Person
King *king = [[King alloc]init];
king.name = @"国王";