Objective-C 程序设计(第六版)第十章习题答案

1.

 1 - (id) init
 2 {
 3     return [self initWithWidth: 0 andHeight: 0];
 4 }
 5 
 6 - (id) initWithWidth: (int) w andHeight: (int) h
 7 {
 8     self = [super init];
 9     if (self) {
10         [self setWidth: w andHeight: h];
11     }
12     return self;
13 }

2.

 1 - (id)init
 2 {
 3     return [self initWithSide:0];
 4 }
 5 
 6 - (id) initWithSide: (int) side
 7 {
 8     self = [super init];
 9     if (self) {
10         [self setSide:side];
11     }
12     return self;
13 }

3.

 1 //.m文件定义静态变量  增加类方法
 2 #import "Fraction.h"
 3 
 4 static int gCounter;
 5 
 6 @implementation Fraction
 7 @synthesize numerator, denominator;
 8 
 9 
10 - (Fraction *) add: (Fraction *) f
11 {
12     Fraction *result = [[Fraction alloc] init];
13     
14    result.numerator = numerator * f.denominator + denominator * f.numerator;
15    result.denominator = denominator * f.denominator;
16     
17     [result reduce];
18     gCounter++;
19     return result;
20 }
21 
22 + (int) count
23 {
24     return gCounter;
25 }

4.

 typedef enum {Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday} Day;

5. 枚举类型不知道怎么用

 1         typedef Fraction * FractionObj;
 2         
 3         FractionObj f1 = [[Fraction alloc] init],
 4                     f2 = [[Fraction alloc] init];
 5         
 6         [f1 setTo: 1 over: 2];
 7         [f2 setTo: 2 over: 3];
 8         
 9         [f1 print];
10         [f2 print];

6.略.

转载于:https://www.cnblogs.com/MingMing-King/p/4109427.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值