// cc 文件名.m -framework Foundation 编译链接 #import <Foundation/Foundation.h> //枚举性别 typedef enum{ SexMen, SexWoman, SexUnknow } Sex; //枚举毛色 typedef enum{ FurColorBlack, FurColorRed, FurColorGreen } FurColor; //结构体日期 typedef struct{ int year; int month; int day; } Date; //创建狗类 @interface Dog : NSObject { @public double weigth; //体重 FurColor fcolor; //毛色 } - (void)run; - (void)eat; @end //实现狗类方法 @implementation Dog - (void)run { weigth -= 1; NSLog(@"狗开始跑了,体重减1,现在的体重是:%.2f",weigth); } - (void)eat { weigth += 1; NSLog(@"狗开始吃了,体重加1,现在的体重是:%.2f",weigth); } @end //创建学生类 @interface Student : NSObject { @public char *name; //姓名 Sex sex; //性别 int age; //年龄 double weigth; //体重 Date birthday; //生日 Dog *dog; } - (void)liuDog; - (void)weiDog; - (void)print; @end //实现学生方法 @implementation Student - (void)print { NSLog(@"姓名:%s,性别:%d,年龄:%d岁,体重:%.2f公斤,出生日期:%d-%d-%d,狗的体重:%.2f,狗的毛色:%d",name,sex,age,weigth,birthday.year,birthday.month,birthday.day,dog->weigth,dog->fcolor); } -(void)liuDog { [dog run]; } - (void)weiDog { [dog eat]; } @end int main() { Dog *zsDog = [Dog new]; zsDog->weigth = 20; zsDog->fcolor = FurColorBlack; Student *zs =[Student new]; zs->name = "jack"; //??中文赋值时无输出?? zs->sex = SexMen; zs->age = 20; zs->weigth = 75.3; Date d = {2010,10,5}; zs->birthday = d; zs->dog =zsDog; [zs print]; [zs liuDog]; [zs weiDog]; return 0; }
时间: 2024-10-12 09:02:17