温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

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

发布时间:2020-06-16 16:18:14 来源:网络 阅读:299 作者:M了个J 栏目:开发技术

举个例子

大家会使用类并且会声明并且实现类的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;

}


向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI