黑马程序员—OC学习笔记—类方法与实例方法解析

———–Java培训、Android培训、IOS培训、.Net培训、期待与您交流!————
本节是个人学习过程中的笔记,供初学者一起学习,欢迎大家批评指正,留言参与讨论,谢谢。
本节内容,类方法与实例方法解析,以及他们间相互调用,代码如下:

#import <Foundation/Foundation.h>
#include <stdio.h>

typedef enum  //定义计算器的枚举类型
{
    简单计算器,
    科学计算器,
    编程计算器
} Type;

@interface Calculator : NSObject
{
    double _length;//实体计算器的长宽和面积,以及类型
    double _width;
    double _area;
    Type _type;
}

- (void)setLength :(double)length andWidth: (double)width;
- (void)setType :(Type)type;

- (double)length;
- (double)width;
- (double)area;
- (Type)type;

+ (double)sumOfNum1 :(double)num1 AndNum2: (double)num2;
+ (double)marginOfNum1 :(double)num1 AndNum2: (double)num2;
+ (double)productOfNum1 :(double)num1 AndNum2: (double)num2;
+ (double)quotientOfNum1 :(double)num1 AndNum2: (double)num2;
+ (double)averageOfNum1 :(double)num1 AndNum2: (double)num2;
@end

@implementation Calculator

- (void)setLength :(double)length andWidth: (double)width
{
    _length = length;
    _width = width;
}

- (void)setType :(Type)type { _type = type;}


- (double)length { return _length;}
- (double)width { return _width;}
- (Type)type { return _type;}

- (double)area
{
    _area = [Calculator productOfNum1: _length AndNum2: _width];
    //这里能够使用,是因为在类接口里面,都有声明
    //对象方法,调用类方法,但是不能反过来
    return _area;
}



+ (double)sumOfNum1 :(double)num1 AndNum2: (double)num2
{
    return num1 + num2;
}

+ (double)marginOfNum1 :(double)num1 AndNum2: (double)num2
{
    return num1 - num2;
}

+ (double)averageOfNum1 :(double)num1 AndNum2: (double)num2
{
    return [Calculator sumOfNum1: num1 AndNum2: num2]/2;
    //类方法之间的调用,但是禁止双向调用;递归调用时候更加谨慎。
}

+ (double)productOfNum1 :(double)num1 AndNum2: (double)num2
{
    return num1 * num2;
}

+ (double)quotientOfNum1 :(double)num1 AndNum2: (double)num2
{
    return num1 / num2;
}

@end

void testCalculator()
{
    //测试类方法的调用,此时计算器作为一个工具类
    double num1, num2;
    char operator;
    double result;

    NSLog(@"请输入您要计算的两个数值和需要的运算符,运算符包括“+,-,*,/,^”,其中^代表求平均值\n以格式“数1 运算符 数2”来输入,中间空格间隔,输入回车结束输出,获得计算结果!");

    scanf("%lf %c %lf",&num1,&operator,&num2);

    NSLog(@"您输入:%f %c %f,计算器开始计算",num1,operator,num2);

    switch (operator) {  //case后面如果有多个句子,一定要大括号包起来成一个块
        case '+':
        {
            result = [Calculator sumOfNum1: num1 AndNum2: num2];
            NSLog(@"%f %c %f = %f",num1,operator,num2,result);
            break;
        }

        case '-':
        {
            result = [Calculator marginOfNum1: num1 AndNum2: num2];
            NSLog(@"%f %c %f = %f",num1,operator,num2,result);
            break;
        }

        case '*':
        {
            result = [Calculator productOfNum1: num1 AndNum2: num2];
            NSLog(@"%f %c %f = %f",num1,operator,num2,result);
            break;
        }

        case '/':
        {
            result = [Calculator quotientOfNum1: num1 AndNum2: num2];
            NSLog(@"%f %c %f = %f",num1,operator,num2,result);
            break;
        }

        case '^':
        {
            result = [Calculator averageOfNum1: num1 AndNum2: num2];
            NSLog(@"%f %c %f = %f",num1,operator,num2,result);
            break;
        }

        default:
        {
            NSLog(@"您的输入不规范,计算结束!");
            break;
        }
    }

}

Calculator* createCalculator()
{
    Calculator* newCalculator = [Calculator new];
    [newCalculator setLength: 10.5 andWidth: 5.5];
    [newCalculator setType: 编程计算器];
    NSLog(@"新建的%d号计算器面积为 %f",[newCalculator type],[newCalculator area]);
    return newCalculator;

}

int main()
{
    testCalculator();
    createCalculator();
    return 0;
}

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值