类与类之间调用Computer-CPU-Mouse--看懂它看懂类之间的调用

举个例子

大家会使用类并且会声明并且实现类的setter 和 getter方法

那让我们来更深入的学习,类中包含另一个类的实现方法吧。


题目:

Computer类(电脑类)

该类用于描述一个具体的电脑类,可以对该类进行一些基本的操作

属性:

1. 电脑品牌   

2. 鼠标   

3. CPU  


方法:

1、成员变量的set、get方法

2、电脑信息的详细描述 包括电脑品牌 鼠标信息,CPU信息等等


鼠标类:

  1. 鼠标品牌
  2. 类别
  3. 价格

CPU类:

  1. CPU型号
  2. 缓存
  3. 价格

解题思路:(有很多种,但大单位为程序员交流方便都做了一些规定,我们还是随大流,让大家养成一个良好的书写习惯。)

定义三个类,因为并没有完全一样的属性可以抽取而只是包含的关系

电脑有cpu和鼠标,但cpu和鼠标不是电脑,所以电脑应该是包含cpu和鼠标,而不是继承。

在电脑属性中包含cpu属性和鼠标属性即可(当然还可以后其他属性,只是举例说明,其他的就不列举了)

在三个类中分别实现它的getter和setter

在电脑类中实现输出电脑详细信息的方法(- (void)showAllDetail;)

以下是实现和main函数,声明略去


#import <Foundation/Foundation.h>

#import "Mouse.h"

#import "CpuType.h"

//        Computer(电脑类)

//        该类用于描述一个具体的电脑类,可以对该类进行一些基本的操作

//        属性:

//        1. 电脑品牌

//        2. 鼠标

//        3. CPU

@interface Computer : NSObject

{

    char * _brand;

    Mouse * _mouse;

    CpuType *_cpu;

}


// setmethod

- (void)setBrand:(char *)brand;

- (void)setMouse:(Mouse *)mouse;

- (void)setCpuType:(CpuType *)cpu;



// get method

- (char *)brand;

- (Mouse *)mouse;

- (CpuType *)cpu;



// 电脑信息的详细描述 包括电脑品牌 鼠标信息,CPU信息等等

// opration method

- (void)showAllDitatil;

@end


#import "Mouse.h"


@implementation Mouse

// set method

- (void)setBrand:(char *)brand{

    _brand = brand;

}

- (void)setType:(char *)type{

    _type = type;

}

- (void)setPrice:(float)price{

    _price = price;

}


// get method

- (char *)brand{

    return _brand;

}

- (char *)type{

    return _type;

}

- (float)price{

    return _price;

}


@end


#import "CpuType.h"


@implementation CpuType

// set method

- (void)setType:(char *)type{

    _type = type;

}

- (void)setCache:(int)cache{

    _cache = cache;

}

- (void)setPrice:(float)price{

    _price = price;

}



// get method

- (char *)type{

    return _type;

}

- (int)cache{

    return _cache;

}

- (float)price{

    return _price;

}

@end



#import <Foundation/Foundation.h>

#import "Computer.h"


int main(int argc, const char * argv[]) {

    @autoreleasepool {

//        练习2

//        Computer(电脑类)

//        该类用于描述一个具体的电脑类,可以对该类进行一些基本的操作

//        属性:

//        1. 电脑品牌

//        2. 鼠标

//        3. CPU

//        

//        方法:

//        1、成员变量的setget方法

//        2、电脑信息的详细描述 包括电脑品牌 鼠标信息,CPU信息等等

//        

//        鼠标类:

//        鼠标品牌

//        类别

//        价格

//        CPU类:

//        CPU型号

//        缓存

//        价格

        

        // 1.定义电脑对象

        Computer *computer = [[Computer alloc]init];

        char *comBrand = "lenovo";

        [computer setBrand:comBrand];

        

        // 2.定义鼠标

        Mouse * mouse = [[Mouse alloc]init];

        char * mouseBrand = "jack";

        char * mouseType = "hit";

        float mousePrice = 1000;

        [mouse setBrand:mouseBrand];

        [mouse setType:mouseType];

        [mouse setPrice:mousePrice];

        [computer setMouse:mouse];

        

        // 3.定义CPU

        CpuType *cpu = [[CpuType alloc]init];

        char *cpuType = "UFO";

        int cpuCache = 199;

        float cpuPrice = 788.00;

        [cpu setType:cpuType];

        [cpu setCache:cpuCache];

        [cpu setPrice:cpuPrice];

        [computer setCpuType:cpu];

        

        // 4.打印信息

        [computer showAllDitatil];

        

    }

    return 0;

}



  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值