码迷,mamicode.com
首页 > 其他好文 > 详细

[Objective-c 基础 - 1.2] OC的基本类

时间:2015-05-13 21:41:32      阅读:98      评论:0      收藏:0      [点我收藏+]

标签:

1 #import <Foundation/Foundation.h>
  2 
  3 typedef enum {GenderMan, GenderFemale} Gender;
  4 
  5 typedef enum {ColorRed, ColorBlue, ColorGreen} Color;
  6 
  7 typedef struct
  8 {
  9     int year;
 10     int month;
 11     int day;
 12 } Date;
 13 
 14 
 15 @interface Dog : NSObject
 16 {
 17 @public
 18     float weight;
 19     Color color;
 20 }
 21 - (void) eat;
 22 - (void) run;
 23 
 24 @end
 25 
 26 
 27 @interface Student : NSObject
 28 {
 29     @public
 30     char *name;
 31     Gender gender;
 32     Date birthday;
 33     double weight;
 34     Color favariteColor;
 35     Dog *dog;
 36 }
 37 
 38 - (void) eat;
 39 - (void) run;
 40 - (void) walkDog;
 41 - (void) feedDog;
 42 - (void) print;
 43 
 44 @end
 45 
 46 @implementation Student
 47 - (void) eat
 48 {
 49     weight++;
 50     NSLog(@"吃吃吃,体重增加了1KG, 现在体重是%f", weight);
 51 }
 52 
 53 - (void) run
 54 {
 55     weight--;
 56     NSLog(@"跑跑跑,体重减去了1KG,现在体重是%f", weight);
 57 }
 58 
 59 - (void) print
 60 {
 61     NSLog(@"这个学生的资料-》姓名:%s, 性别:%d, 生日:%d-%d-%d, 体重:%f, 喜爱的颜色:%d", name, gender, birthday.year, birthday.month, birthday.day, weight, favariteColor);
 62 }
 63 
 64 - (void) walkDog
 65 {
 66     [dog run];
 67 }
 68 
 69 - (void) feedDog
 70 {
 71     [dog eat];
 72 }
 73 @end
 74 
 75 
 76 @implementation Dog
 77 - (void) eat
 78 {
 79     NSLog(@"喂狗啦!!!");
 80     weight++;
 81     NSLog(@"狗狗吃吃吃,体重增加了1KG, 现在体重是%f", weight);
 82 }
 83 
 84 - (void) run
 85 {
 86     NSLog(@"遛狗啦!!!!");
 87     weight--;
 88     NSLog(@"狗狗跑跑跑,体重减去了1KG,现在体重是%f", weight);
 89 }
 90 @end
 91 
 92 int main()
 93 {
 94     Student *stu = [Student new];
 95     stu->name = "Jack";
 96     stu->gender = GenderMan;
 97     Date d = {1989, 8, 10};
 98     stu->birthday = d;
 99     stu->weight = 50;
100     stu->favariteColor = ColorRed;
101    
102     Dog *dog = [Dog new];
103     stu->dog = dog;
104    
105     [stu eat];
106     [stu feedDog];
107     [stu print];
108     [stu walkDog];
109     [stu feedDog];
110    
111     return 0;
112 }
技术分享

 

[Objective-c 基础 - 1.2] OC的基本类

标签:

原文地址:http://www.cnblogs.com/wvqusrtg/p/4501371.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!