f(sixleaves) = sixleaves

重剑无锋 大巧不工

  C++博客 :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  95 随笔 :: 0 文章 :: 7 评论 :: 0 Trackbacks
 1 #import <Foundation/Foundation.h>
 2 
 3 
 4 @interface Animal : NSObject
 5 {
 6     
 7     int _age;
 8     double _weight;
 9 }
10 
11 - (void)setAge:(int)age;
12 - (int)age;
13 
14 - (void)setWeight:(double)weight;
15 - (double)weight;
16 
17 @end
18 
19 
20 @implementation Animal
21 
22 - (void)setAge:(int)age
23 {
24     _age = age;
25 }
26 - (int)age
27 {
28     return _age;
29 }
30 
31 - (void)setWeight:(double)weight
32 {
33     _weight = weight;
34 }
35 - (double)weight
36 {
37     return _weight;
38 }
39 
40 @end
41 
42 @interface Dog : Animal
43 @end
44 
45 @implementation Dog
46 @end
47 
48 @interface Cat : Animal
49 @end
50 @implementation Cat
51 @end
52 
53 int main() {
54     
55     Dog *d = [Dog new];
56     d.age = 10;
57     
58     Cat *c = [Cat new];
59     c.age = 20;
60     
61     NSLog(@"dog's age = %d, cat's age = %d", d.age, c.age);
62     
63     
64     return 0;
65 }
66 /*
67     什么叫继承:如代码可得出
68     1.如果A继承自B,那么A就含有B的所有成员变量和方法,这就叫继承。
69     
70     好处:
71     1.可以提高代码复用,把相同代码抽出来,如抽出Animal类,让Dog、Cat继承
72     2.建立了类之间的关系,如Animal是Dog与Cat的父类
73     
74     
75     Tips:OC中有两个根类,一个是NSObject、一个是NSProxy。
76          继承自NSObject可以让类能调用new创建对象,或者调用alloc、init。
77 
78 */
posted on 2015-04-30 15:59 swp 阅读(108) 评论(0)  编辑 收藏 引用 所属分类: objective-c

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理