Objective-C 学习笔记 04 - 继承和对象的实例
来源:互联网 发布:淘宝回购率权重 编辑:程序博客网 时间:2024/05/16 11:53
新建类Employee继承于Person并为Employee添加ID属性。
Employee.h
#import "Person.h"@interface Employee : Person{ int employeeID;}@property int employeeID;@endEmployee.m
#import "Employee.h"@implementation Employee@synthesize employeeID;@end测试:
Employee *employee = [[Employee alloc] init]; [employee setWeightInKilos:96]; [employee setHeightInMeters:1.8]; [employee setEmployeeID:15]; NSLog(@"Employee id is %d", [employee employeeID]);
2015-01-29 20:19:41.702 BMITime[1476:19162] Employee id is 15
覆盖方法
子类可以覆盖父类同名的方法,如下修改,使得Employee的bodyMassIndex始终返回19。
#import "Employee.h"@implementation Employee@synthesize employeeID;- (float)bodyMassIndex{ return 19.0;}@end
super
在子类中使用super指令可以调用父类的方法
#import "Employee.h"@implementation Employee@synthesize employeeID;- (float)bodyMassIndex{ float normalBMI = [super bodyMassIndex]; return normalBMI * 0.9;}@end执行结果如下:
2015-01-29 20:25:16.297 BMITime[1771:22612] Employee id is 15 bmi is 26.666668
对象所有权和ARC
添加Asset类,用于记录公司发给员工的物品,如PC等,并修改Employee类,增加一个NSMutableArray对象用于存放某个员工的物品对象。
Asset.h
#import <Foundation/Foundation.h>@interface Asset : NSObject{ NSString *label; unsigned int resaleValue;}@property (strong) NSString *label;@property unsigned int resaleValue;@endAsset.m,重载了description和dealloc方法,便于查看析构函数调用的情况
#import "Asset.h"@implementation Asset@synthesize label, resaleValue;- (NSString *)description{ return [NSString stringWithFormat:@"<%@: $%d >", [self label], [self resaleValue]];}- (void)dealloc{ NSLog(@"deallocating %@", self);}@end修改Employee类,添加存放Asset的数组并实现添加asset和计算asset价值的方法
Employee.h,@class Asset;类似于c++的前向声明。
#import "Person.h"@class Asset;@interface Employee : Person{ int employeeID; NSMutableArray *assets;}@property int employeeID;- (void)addAssetsObject:(Asset *)a;- (unsigned int)valueOfAssets;@endEmployee.m
#import "Employee.h"#import "Asset.h"@implementation Employee@synthesize employeeID;- (float)bodyMassIndex{ float normalBMI = [super bodyMassIndex]; return normalBMI * 0.9;}- (void)addAssetsObject:(Asset *)a{ if (!assets) { assets = [[NSMutableArray alloc] init]; } [assets addObject:a];}- (unsigned int)valueOfAssets{ unsigned int sum = 0; for (Asset *a in assets) { sum += [a resaleValue]; } return sum;}- (NSString *)description{ return [NSString stringWithFormat:@"<Empoyee %d: $%d in assets>", [self employeeID], [self valueOfAssets]];}- (void)dealloc{ NSLog(@"deallocating %@", self);}@end添加测试代码,生成10个员工,生成10个asset,随机分发给这10个员工。删除员工5,员工5所拥有的asset会自动释放。最后放弃存放所有员工的数组的所有权,所有剩余的员工以及asset都会被释放。
NSMutableArray *employees = [[NSMutableArray alloc] init]; for (int i = 0; i < 10; i++) { Employee *person = [[Employee alloc] init]; [person setWeightInKilos:90 + i]; [person setHeightInMeters:1.8 - i / 10.0]; [person setEmployeeID:i]; [employees addObject:person]; } for (int i = 0; i < 10; i++) { Asset *asset = [[Asset alloc] init]; NSString *currentLable = [NSString stringWithFormat:@"Laptop %d", i]; [asset setLabel:currentLable]; [asset setResaleValue:i * 17]; NSUInteger randomIndex = random() % [employees count]; Employee *randomEmployee = [employees objectAtIndex:randomIndex]; [randomEmployee addAssetsObject:asset]; } NSLog(@"Employees: %@", employees); NSLog(@"Giving up ownership of one employee"); [employees removeObjectAtIndex:5]; NSLog(@"Giving up ownership of array"); employees = nil;执行结果如下:
Employees: ( "<Empoyee 0: $0 in assets>", "<Empoyee 1: $153 in assets>", "<Empoyee 2: $119 in assets>", "<Empoyee 3: $68 in assets>", "<Empoyee 4: $0 in assets>", "<Empoyee 5: $136 in assets>", "<Empoyee 6: $119 in assets>", "<Empoyee 7: $34 in assets>", "<Empoyee 8: $0 in assets>", "<Empoyee 9: $136 in assets>")Giving up ownership of one employeedeallocating <Empoyee 5: $136 in assets>deallocating <Laptop 3: $51 >deallocating <Laptop 5: $85 >Giving up ownership of arraydeallocating <Empoyee 0: $0 in assets>deallocating <Empoyee 1: $153 in assets>deallocating <Laptop 9: $153 >deallocating <Empoyee 2: $119 in assets>deallocating <Laptop 7: $119 >deallocating <Empoyee 3: $68 in assets>deallocating <Laptop 0: $0 >deallocating <Laptop 4: $68 >deallocating <Empoyee 4: $0 in assets>deallocating <Empoyee 6: $119 in assets>deallocating <Laptop 1: $17 >deallocating <Laptop 6: $102 >deallocating <Empoyee 7: $34 in assets>deallocating <Laptop 2: $34 >deallocating <Empoyee 8: $0 in assets>deallocating <Empoyee 9: $136 in assets>deallocating <Laptop 8: $136 >
0 0
- Objective-C 学习笔记 04 - 继承和对象的实例
- iOS学习笔记-Objective-C-继承和实例化、对象初始化
- 学习笔记(objective-c)-继承
- Objective-C基础学习笔记(四)-面向对象的三大特性之继承
- Objective-C学习笔记---类别(实现多重继承的方法)
- Objective-C学习笔记---类别(实现多重继承的方法)
- Objective-C面向对象-Objective-C 对象,实例和方法
- Objective-C - 继承的经典实例
- Objective-C学习笔记第四章继承
- Objective-C学习笔记九:继承一
- Objective-C学习笔记十:继承二
- Objective-C学习笔记2:类、对象和方法
- iOS学习笔记-Objective-C-类和对象
- 【Objective-C】07-OC对象的继承
- Objective-C学习笔记(十九)——对象方法和类方法的相互调用
- Objective-C学习笔记(3)----浅谈 Objective-C 下对象的初始化
- objective-c语法学习:类的继承
- Objective-C 学习笔记 01 - 对象
- 滚动条的使用1
- 数据库管理系统设计环境配置(IIS信息服务管理器&Visual Studio 2008&SQL2005&SQL Management)及sa登陆问题
- Android Services 学习笔记
- javascript笔记
- 通过Intent 打开系统级应用
- Objective-C 学习笔记 04 - 继承和对象的实例
- JSP接口浅析
- 工作总结_4
- MVC-------Model层的不可或缺
- python学习:链式比较
- HttpJspBase 源代码
- 普林斯顿大学学习氛围
- BZOJ 1492 斜率优化dp && cdq分治
- CF 508C(Anya and Ghosts-贪心)