1 #import <Foundation/Foundation.h>
2
3 @interface Person : NSObject
4
5 @property int age;
6 @property NSString * name;
7
8 - (id)initWithName:(NSString *)name andAge:(int)age;
9 @end
Person.m
1 #import "Person.h"
2 @implementation Person
3
4 - (id)initWithName:(NSString *)name andAge:(int)age
5 {
6
7 if (self = [super init]) {
8
9 _name = name;
10 _age = age;
11
12 }
13 return self;
14 }
15 @end
Student.h
1 #import <Foundation/Foundation.h>
2 #import "Person.h"
3
4
5 @interface Student : Person
6
7 @property int no;
8
9 - (id)initWithName:(NSString *)name andAge:(int)age andNo:(int)no;
10
11 @end
Student.m
1 #import "Student.h"
2
3 @implementation Student
4
5 - (id)initWithName:(NSString *)name andAge:(int)age andNo:(int)no
6 {
7
8 if (self = [super initWithName:name andAge:age])
9 {
10 _no = no;
11 }
12 return self;
13
14 }
15
16 @end
main.m
1 #import <Foundation/Foundation.h>
2 #import "Person.h"
3 #import "Student.h"
4
5 int main()
6 {
7
8 Student *s = [[Student alloc] initWithName:@"suweipeng" andAge:24 andNo:211106435];
9 NSLog(@"name is:%@ ,age is:%i, number is:%i", s.name, s.age, s.no);
10 return 0;
11 }
12
13 /*
14 鎬葷粨:
15 鏋勯犳柟娉曠殑璁捐鍘熷垯錛?br />16 鐖剁被鐨勫睘鎬т氦涓埗綾繪瀯閫犳柟娉曞鐞嗭紝瀛愮被鐨勬垚鍛樺彉閲忕敱瀛愮被鏋勯犳柟娉曞鐞嗐?br />17
18
19 @implementation Student
20 - (id)initWithName:(NSString *)name andAge:(int)age andNo:(int)no
21 {
22
23 if (self = [super initWithName:name andAge:age])
24 {
25 _no = no;
26 }
27 return self;
28
29 }
30 @end
31
32 濡傛灉鎶婅繖孌典唬鐮佹敼鎴愬涓嬬殑鍧忓鏄紝濡傛灉Person綾諱腑鐨刟ge鍚嶅瓧鍙樹簡錛屽湪瀛愮被涓繀欏誨緱鍋氭洿鏀廣?br />33 浠g爜鐨勮﹀悎鎬у己銆備絾濡傛灉鎴戜滑鐢ㄤ笂闈㈢殑浠g爜鏉ュ疄鐜幫紝鍒欏瓙綾諱笉鐢ㄥ彉鍖栵紝鐖剁被鎬庝箞鍙樺寲鏄埗綾昏嚜宸?br />34 鐨勪簨鎯咃紝榪欏叾瀹炰篃灝辨槸鍗曚竴鑱岃矗~銆?br />35
36 @implementation Student
37 - (id)initWithName:(NSString *)name andAge:(int)age andNo:(int)no
38 {
39
40 if (self = [super init])
41 {
42 _no = no;
43
44 self.age = age; // 鏈川涓婃槸璋冪敤setter涓巊etter鏂規硶銆備絾鏄痵etAge榪欎釜鏂規硶鍦?br />45 self.no = no; // 瀛愮被涓槸涓嶅瓨鍦ㄧ殑錛屾墍浠ュ畠浼氭部鐫superclass鎸囬拡緇х畫鎵懼埌鍏?br />46 // 鐖剁被錛岀劧鍚庤皟鐢ㄧ埗綾葷殑setAge鏂規硶錛屾墍浠ヨ繖涓ゅ彞鍏跺疄涔熷彲浠ユ敼鎴?br />47 // super.age = age, super.no = no;浣嗕笉寤鴻榪欎箞鍐欍?br />48 // 紼嬪簭鐨勮﹀悎鎬уお寮恒?br />49 }
50 return self;
51 }
52 @end
53
54 */

]]>