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

———–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];
}


@end

void 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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值