本帖最后由 talent123 于 2015-5-29 23:48 编辑
- #import <Foundation/Foundation.h>
- @interface Person : NSObject
- {
- int _age;
- NSString *_name;
- }
- - (void)setAge:(int)age;
- - (int)age;
- - (void)print;
- - (id)init;
- - (id)initWithName:(NSString *)name andAge:(int)age;
- @end
- @implementation Person
- - (id)initWithName:(NSString *)name andAge:(int)age
- {
- self = [super init];
- if (self != nil)
- {
- _name = name;
- _age = age;
- }
- return self;
- }
- - (id)init
- {
- self = [super init];
- if (self != nil)
- {
- _age = 10;
- }
- return self;
- }
- - (void)setAge:(int)age
- {
- _age = age;
- }
- - (int)age
- {
- return _age;
- }
- - (void)print
- {
- NSLog(@"%d---%@--person",_age,_name);
- }
- @end
- @interface Student : Person
- {
- int _no;
- }
- - (id)initWithName(NSString *)name andAge:(int)age andN0:(int)no;
- @end
- @implementation Student
- - (id)initWithName(NSString *)name andAge:(int)age andN0:(int)no
- {
- self = [super initWithName:name andAge:age];
- if (self != nil)
- {
- _no = no;
- }
- return self;
- }
- @end
- int main (int argc, const char *argv[])
- {
- Person *p = [[Person alloc] initWithName:@"jack" andAge:10];
- /*[p setAge:10];*/
- [p print];
- Student *stu = [[Student alloc] initWithName:@"Tom" andAge:12 andNo:2];
- [stu print];
- return 0;
- }
复制代码 - (id)initWithName(NSString *)name andAge:(int)age andN0:(int)no;
说我这行不对..还有
- (id)initWithName(NSString *)name andAge:(int)age andN0:(int)no
也就是 53 和57行
发现错误了..少一个冒号NO也大写了!!!我的天呐!!真对不起老师。
搜索
复制
|
|