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;@end
Employee.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;@end
Asset.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;@end
Employee.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
原创粉丝点击