// // Created by zxl on 15/9/4. // Copyright (c) 2015年 zxl. All rights reserved. //
//父类声明 #import <Foundation/Foundation.h>
@interface Person : NSObject
@property NSString* name; @property int age; -(instancetype) initWithName:(NSString *) name andAge:(int) age;
@end //父类实现。 init来自NSObject类型 #import "Person.h"
@implementation Person
-(instancetype) initWithName:(NSString *)name andAge:(int)age{ if(self =[super init]){ _name=name; _age=age;
} return self; } @end
//声明属性和方法。 #import <Foundation/Foundation.h> #import "Person.h" //记得导头文件。 @interface Student : Person @property int sno; -(instancetype) initWithName:(NSString *)name andAge:(int)age andSno:(int) sno;
@end //实现构造方法。用super调用父类的构造方法,返回给self。 #import "Student.h"
@implementation Student //以initWith开始。返回值类型instancetype -(instancetype) initWithName:(NSString *)name andAge:(int)age andSno:(int) sno{ //忘记 打中括号了。 if(self=[super initWithName:name andAge:age]){ _sno=sno; } //返回值又忘记写了。 return self; }
@end //给构造方法传递三个参数。
Student *stu= [[Student alloc]initWithName:@"Durant" andAge:27 andSno:9527]; NSLog(@"%@,%d,%d",stu.name,stu.age,stu.sno );
|