OC继承专题

FJEmployee.h

#import <Foundation/Foundation.h>

//objectC创建枚举类型;

typedef NS_ENUM(NSUInteger,FJEmployeeType ){

    FJEmployeeTypeManager,

    FJEmployeeTypeEngineer,

    FJEmployeeTypeSalesman} ;


@interface FJEmployee : NSObject

/**这是一个父类,员工的共同属性姓名和工资*/

@property (nonatomic,copy) NSString *name;

@property (nonatomic,readonly) double salary;


/**隐藏多种员工子类型的细节,只暴露员工接口给用户调用*/

+ (instancetype) employeeWithType:(FJEmployeeType) typeName;


@end

FJEmployee.m

#import "FJEmployee.h"

#import "FJManager.h"

#import "FJEngineer.h"

#import "FJSaledman.h"

@implementation FJEmployee

//类簇(class - cluster

//GoF设计模式:静态工厂方法模式

//GoF设计模式(23种设计模式)

//创建型模式:单例,工厂

//结构型模式:适配器,门面,装潢,桥梁                        

//行为型模式:观察者,代理

//单一职责,开闭原则,依赖倒转原则,里氏替换原则,接口隔离原则,合成聚合复用,迪米特原则

+ (instancetype)employeeWithType:(FJEmployeeType)typeName{

    FJEmployee *employee = nil;

    switch (typeName) {

        case FJEmployeeTypeManager:

            employee = [[FJManager alloc]init];

            break;

        case FJEmployeeTypeEngineer:

            employee = [[FJEngineer alloc] init];

            break;

        case FJEmployeeTypeSalesman:

            employee = [[FJSaledman alloc]init];

            //employee = [FJSaledman new];new表示用默认的初始化方法,带参数的初始化方法就不可以使用new

            break;

    }

    return employee;

}


FJManager.h

#import "FJEmployee.h"

//#import "FJEmployee.h"

@interface FJManager : FJEmployee




@end

FJManager.m

#import "FJManager.h"

/**部门经理的工资固定为10000*/

@implementation FJManager




- (double)salary{

    return 10000;

}


@end

FJEngineer.h

#import "FJEmployee.h"

/**工程师根据工作时间计算工资*/

@interface FJEngineer : FJEmployee

@property (nonatomic,strong) NSNumber *workinghour;




@end

FJEngineer.m

#import "FJEngineer.h"


@implementation FJEngineer



/**假设每个小时200*/

- (double)salary{

    return  200*_workinghour.doubleValue;

}

@end

FJSalsman.h

#import "FJEmployee.h"



@interface FJSaledman : FJEmployee

/**销售人员根据销售额提成*/


@property (nonatomic,strong) NSNumber *sales;

@end

FJSalaman.m

#import "FJSaledman.h"


@implementation FJSaledman



-(double)salary{

    

    return 1500 + _sales.doubleValue*0.05;

}

@end



main.m

#import <Foundation/Foundation.h>

#import "FJManager.h"

#import "FJSaledman.h"

#import "FJEngineer.h"

int main(int argc, const char * argv[]) {

    @autoreleasepool {

        

        NSArray *names = @[@"luhan",@"fanjuan",@"qianxi"];

    

        NSMutableArray *empArray = [NSMutableArray array];

        

        for (NSString *name in names) {

           

        FJEmployee *emp = [FJEmployee employeeWithType:arc4random() % 3];

        

            emp.name = name;

            [empArray addObject:name];

            

        }

        

        for (FJEmployee *emp in empArray) {

            

            if ([emp isKindOfClass:[FJEngineer class]]) {

                

                printf("请输入%s的工作时间:",emp.name.UTF8String);

                int hour;

                scanf("%d",&hour);

                //通过选择器去执行对象的方法

        [emp performSelector:@selector(setWorkinghour:) withObject:@(hour)];

            

            }

               else if([emp isKindOfClass:[FJSaledman class]]){

                   

                printf("请输入%s的销售额:\n",emp.name.UTF8String);

                double sales;

                scanf("%lf",&sales);

               

            [emp performSelector:@selector(setSales:) withObject:@(sales)];

                

            }

            printf("%s本月的工资是$%lf\n",emp.name.UTF8String,emp.salary);

        }

        

    }

    return 0;

}



你可能感兴趣的:(OC继承专题)