黑马程序员—OC学习笔记—OOP中一个类包含另外一个类的对象

来源:互联网 发布:王良正射鱼器淘宝 编辑:程序博客网 时间:2024/06/02 11:48

———–Java培训、Android培训、IOS培训、.Net培训、期待与您交流!————
本节是个人学习过程中的笔记,供初学者一起学习,欢迎大家批评指正,留言参与讨论,谢谢。
本节内容,OOP中一个类包含另外一个类的对象,其实就是学生拥有宠物狗,这句话的延伸实现,代码如下:

#import <Foundation/Foundation.h>//#include <stdio.h>/*学生类 设计        属性(成员变量或实例变量)学号,姓名,性别,年龄,身高,体重,生日,最喜欢颜色,宠物狗        方法 吃饭、喂狗、跑步、遛狗、自我介绍、介绍宠物狗*/typedef enum{ //年-月-日 格式的日期    SexBoy,    SexGirl} Sex;typedef struct{ //年-月-日 格式的日期    int year;    int month;    int day;} Date;typedef enum{//常见色系的赤橙黄绿青蓝紫    ColorBlack,    ColorRed,    ColorWhite,    ColorBrown,    ColorGray,    ColorYellow,    ColorBlue,    ColorPurple,    ColorOrange,    ColorCyan,    ColorGreen}Color;/*宠物狗的设计 属性:昵称、品种、年龄、性别、毛色、体重 方法:吃饭、遛弯 */typedef enum{//几种常见的狗    牧羊犬,    吉娃娃,    俊介,    狼狗,    史努比,    哮天犬,    黑皇}DogType;@interface PetDog : NSObject{    @public    char* name;    int age;    DogType type;    Sex dogSex;    Color purColor;    double weight;}-(void) eat;-(void) run;@end@implementation PetDog-(void) eat{    weight++;    NSLog(@"PetDog %s is eatting, weight = %.2f",name,weight);    //同时喂狗}-(void) run{    weight-=0.5;    NSLog(@"PetDog %s is running, weight = %.2f",name,weight);    //同时遛狗}@end@interface Student : NSObject{    @public    int no;    char* name;    Sex sex;    int age;    double height;    double weight;    Date birthday;    Color preferColor;    PetDog *petDog;}-(void) eat;-(void) run;-(void) selfDescribe;//以下方法需要狗对象的存在,所以测试时候,必须注意-(void) petdogDescribe;-(void) feedDog;-(void) runDog;@end@implementation Student// : NSObject //先写interface,不然容易把NSObject乱放-(void) eat{    weight++;    height+=0.01;    NSLog(@"Student %s is eatting, weight = %.2f,height = %.2f",name,weight,height);    //同时喂狗,可以实现,但是耦合性太高}-(void) run{    weight-=0.5;    height+=0.01;    NSLog(@"Student %s is running, weight = %.2f,height = %.2f",name,weight,height);    //同时遛狗,可以实现,但是耦合性太高}-(void) selfDescribe{    NSLog(@"我叫 %s 是%d岁,最喜欢的颜色是 %d",name,age,preferColor);}-(void) petdogDescribe  //这个方法,必须在宠物狗对象创建之后才能调用{    NSLog(@"我的宠物狗 %s 是%d岁,品种是 %d,性别是 %d,颜色是 %d",          petDog->name,petDog->age,petDog->type,petDog->dogSex,petDog->purColor);}-(void) feedDog{    NSLog(@"学生 %s 在喂自己的宠物狗 %s",name,petDog->name);    [petDog eat];}-(void) runDog{    NSLog(@"学生 %s 在溜自己的宠物狗 %s",name,petDog->name);    [petDog run];}@endvoid testStudent(){    Student *tiger = [Student new];    tiger->no = 1;    tiger->age = 20;    tiger->name = "Tiger";  //这一句成功?    tiger->sex = SexBoy;    tiger->height = 1.78;    tiger->weight = 110.8;    Date birth = {1990,1,1};    tiger->birthday = birth;    tiger->preferColor = ColorBlue;    [tiger eat];    [tiger run];    [tiger selfDescribe];   // [tiger petdogDescribe]; 此处测试是无意义的,因为还没狗   // Student *pander = [Student new];}void testPetDog(){    PetDog *testDog = [PetDog new];    testDog->age = 3;    //char *temp = "旺财";    testDog->name = "Wangcai";  //中文作为字符串内容,不能输出?问题?    testDog->dogSex = SexBoy;    testDog->weight = 10.8;    testDog->purColor = ColorBrown;    testDog->type = 黑皇;    [testDog eat];    [testDog run];     NSLog(@"%s 是%d岁,品种是 %d,性别是 %d,颜色是 %d",testDog->name,testDog->age,testDog->type,testDog->dogSex,testDog->purColor);    ////中文作为字符串内容,不能输出,NSLog问题//    char *temp = "旺财";//    char temp1[] = "旺财";//    NSLog(@"%s 前面有中文",temp);//    printf("%s\n",temp);//    NSLog(@"%s 前面有中文",temp1);//    printf("%s\n",temp1);}void testTogether(){    Student *tiger = [Student new];    tiger->no = 1;    tiger->age = 20;    tiger->name = "Tiger";  //这一句成功?    tiger->sex = SexBoy;    tiger->height = 1.78;    tiger->weight = 110.8;    Date birth = {1990,1,1};    tiger->birthday = birth;    tiger->preferColor = ColorBlue;    PetDog *testDog = [PetDog new];    testDog->age = 3;    testDog->name = "Wangcai";  //中文作为字符串内容,不能输出?问题?    testDog->dogSex = SexBoy;    testDog->weight = 10.8;    testDog->purColor = ColorBrown;    testDog->type = 牧羊犬;    tiger->petDog = testDog; //人和宠物狗的配对,对象的绑定    [tiger petdogDescribe];    [tiger feedDog];    [tiger runDog];    // Student *pander = [Student new];}int main(){    testStudent();    testPetDog();    testTogether();}

程序运行结果如下:
这里写图片描述

0 0
原创粉丝点击