- /* 学生
- */
- #import <Foundation/Foundation.h>
- typedef enum
- {
- SexMan,
- SexWoman
- }Sex;
- typedef struct
- {
- int year;
- int mouth;
- int day;
- }Date;
- typedef enum
- {
- ColorBlack,
- ColorRed,
- ColorGreen
- }Color;
- @interface Dog : NSObject
- {
- @public
- double weight;
- Color curColor;
- }
- -(void)eat;
- -(void)run;
- @end
- @implementation Dog
- - (void) eat
- {
- weight+=1;
- NSLog(@"狗吃完这次后体重是%f",weight);
- }
- - (void) run
- {
- weight-=1;
- NSLog(@"狗跑完这次体重是%f",weight);
- }
- @end
- @interface Student : NSObject
- {
- @public
- Sex sex;
- Date birthday;
- double weight;
- Color favColor;
- char *name;
- Dog *dog;
- }
- - (void)eat;
- - (void)run;
- - (void)print;
- - (void)liuDog;
- - (void)weiDog;
- @end
- @implementation Student
- - (void) eat
- {
- weight+=1;
- NSLog(@"吃完这次后体重是%f",weight);
- }
- - (void) run
- {
- weight-=1;
- NSLog(@"跑完这次体重是%f",weight);
- }
- -(void) print
- {
- NSLog(@"性别=%d, 颜色=%d, 生日=%d-%d-%d 姓名%s",sex,favColor,birthday.year,birthday.mouth,birthday.day,name);
- }
- - (void)liuDog
- {
- [dog run];
- }
- - (void)weiDog
- {
- [dog eat];
- }
- @end
- int main()
- {
- Student *s = [Student new];
-
- //Dog *d = [Dog new]; 这是视频中的写法,给类中的类赋值,必须用这种方式??
- //d->curColor = ColorGreen;
- //d->weight = 20;
- //s->dog = d;
- s->dog->weight = 100;
- s->dog->curColor = ColorGreen;
- [s liuDog];
- [s weiDog];
-
- return 0;
- }
复制代码
编译连接没有错误,但是执行的时候出现 Segmentation fault: 11的提示 什么意思 |
|