美文网首页
分类和扩展

分类和扩展

作者: 我不白先生 | 来源:发表于2020-09-21 17:00 被阅读0次

1.分类
1.1将一个复杂的类分成若干个模块儿,其中每个模块儿被称为一个分类
1.2分类的作用是降低耦合度
1.3语法
1.4在使用带分类的类时,只能看到主类,不会看到分类
1.5分类中不能定义成员变量或属性
1.6可以给没有源代码的类添加分类
1.7添加到主类中的分类可以被子类继承
1.8要注意:添加到分类中的方法名,不要和主类中的方法名重名,如果重名,主类中的方法将被隐藏


新建分类.png

TRFranction.h

@interface TRFranction : NSObject
@property int n,d;
@end

TRFranction.m

@implementation TRFranction
-(NSString *)description
{
    return [NSString stringWithFormat:@"%d/%d",self.n,self.d];
}
@end

TRFranction+TRInit.h

@interface TRFranction (TRInit)
-(instancetype)initWithN:(int)n andD:(int)d;
+(id)franctionWithN:(int)n andD:(int)d;
@end

TRFranction+TRInit.m

#import "TRFranction+TRInit.h"

@implementation TRFranction (TRInit)
-(instancetype)initWithN:(int)n andD:(int)d
{
    if(d == 0)
    {
        return nil;
    }
    self = [super init];
    if(self)
    {
        self.n = n;
        self.d = d;
    }
    return self;
}
+(id)franctionWithN:(int)n andD:(int)d
{
    __autoreleasing TRFranction *f = [[TRFranction alloc]initWithN:n andD:d];
    return f;
}
@end

TRFranction+TRMath.h

@interface TRFranction (TRMath)
-(TRFranction *)add:(TRFranction *)f;
-(TRFranction *)sub:(TRFranction *)f;
-(TRFranction *)mul:(TRFranction *)f;
-(TRFranction *)div:(TRFranction *)f;
@end

TRFranction+TRMath.m

#import "TRFranction+TRMath.h"
#import "TRFranction+TRInit.h"
@implementation TRFranction (TRMath)
-(TRFranction *)add:(TRFranction *)f
{
    TRFranction *result = [[TRFranction alloc] init];
    result.n = self.n * f.d + self.d * f.n;
    result.d = self.d * f.d;
    return result;
}
-(TRFranction *)sub:(TRFranction *)f
{
    
    int n = self.n * f.d - self.d * f.n;
    int d = self.d * f.d;
    return [TRFranction franctionWithN:n andD:d];
}
-(TRFranction *)mul:(TRFranction *)f
{
    return [TRFranction franctionWithN:self.n * f.n andD:self.d * f.d];
}
-(TRFranction *)div:(TRFranction *)f
{
    return [TRFranction franctionWithN:self.n * f.d andD:self.d * f.n];
}
@end

TRFranction+TRTransfer.h

@interface TRFranction (TRTransfer)
-(double)transfer;
@end

TRFranction+TRTransfer.m

@implementation TRFranction (TRTransfer)
-(double)transfer
{
    return self.n * 1.0 / self.d;
}
@end

NSObject+TRShow.h

@interface NSObject (TRShow)
-(NSString *)tr_print;
@end

NSObject+TRShow.m

@implementation NSObject (TRShow)
-(NSString *)tr_print
{
    return @"这是添加到NSObject类中的方法";
}
@end

ViewController.m

#import "ViewController.h"
#import "TRFranction+TRInit.h"
#import "TRFranction+TRMath.h"
#import "TRFranction+TRTransfer.h"
#import "NSObject+TRShow.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UILabel *outputLabel;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    TRFranction *f1 = [TRFranction franctionWithN:1 andD:2];
    self.outputLabel.text = [NSString stringWithFormat:@"%@",f1];
    TRFranction *f2 = [TRFranction franctionWithN:1 andD:3];
    self.outputLabel.text = [NSString stringWithFormat:@"%@",f2];
    TRFranction *f3 =[f1 add:f2];
    self.outputLabel.text = [NSString stringWithFormat:@"%@",f3];
    TRFranction *f4 =[f1 sub:f2];
    self.outputLabel.text = [NSString stringWithFormat:@"%@",f4];
    TRFranction *f5 =[f1 mul:f2];
    self.outputLabel.text = [NSString stringWithFormat:@"%@",f5];
    TRFranction *f6 =[f1 div:f2];
    self.outputLabel.text = [NSString stringWithFormat:@"%@",f6];
    self.outputLabel.text = [NSString stringWithFormat:@"%g",[f3 transfer]];
    
    self.outputLabel.text = [f3 tr_print];
}
@end

2扩展
2.1扩展的定义形式很像一个没有名字的分类
2.2语法
2.2.1单独写在一个.h文件中,扩展中的成员变量默认是私有的,但是支持修改访问权限。扩展中的属性和方法默认是共有的
2.2.2将扩展写在主类的.m文件中,此时扩展中的成员变量属性和方法都是私有的。
2.3扩展与分类的区别
2.3.1有没有.m文件,扩展没有,分类有
2.3.2有没有成员变量或属性。扩展有;分类没有
2.4扩展与协议的区别:协议可以被任何类采纳但扩展只属于主类

TRFraction.h

@interface TRFraction : NSObject
@property int n,d;
-(id)initWithN:(int)n andD:(int)d;
+(id)franctionWithN:(int)n andD:(int)d;
@end

TRFraction.m

interface TRFraction ()//将扩展写在主类的.m文件中,此时扩展中的成员变量属性和方法都是私有的。
@property int value;//私有的
-(NSString *)extensionMethod0;//私有的

@end
@implementation TRFraction
-(id)initWithN:(int)n andD:(int)d
{
    self = [super init];
    if(self)
    {
        self.n = n;
        self.d = d;
    }
    return self;
}
+(id)franctionWithN:(int)n andD:(int)d
{
    __autoreleasing TRFraction *f = [[TRFraction alloc]initWithN:n andD:d];
    return f;
}
-(NSString *)description
{
    return [NSString stringWithFormat:@"%d/%d",self.n,self.d];
}
-(NSString *)extensionMethod0
{
    return @"扩展中的方法";
}
-(NSString *)extensionMethod1
{
    return @"主类.m文件中的扩展方法";
}
@end

TRFraction+TRMyExtension.h

@interface TRFraction ()
{
    int _a;//默认的访问权限是私有的
    @public//在扩展中,支持访问权限的修改
    int _b;
}
-(NSString *)extensionMethod0;
@end

ViewController.m

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    TRFraction *f = [[TRFraction alloc] init];
    //f->_a = 10;//扩展里的成员变量是可以修改访问权限的;在扩展中成员变量如果是私有的在类里没有访问权限
    f->_b = 20;
    self.outputLabel.text = [f extensionMethod0];
}

相关文章

  • 分类和扩展

    1.分类1.1将一个复杂的类分成若干个模块儿,其中每个模块儿被称为一个分类1.2分类的作用是降低耦合度1.3语法1...

  • 分类和扩展

    1.分类 分类的作用: 可以为原类添加方法(也可以通过runtime的objc_setAssociatedObje...

  • 分类和扩展

    分类Category 分类是运行时动态决议的,扩展是编译期决议的。通过clang -rewrite-objc ma...

  • 分类和扩展

    一般用分类做什么? 1,声明私有方法 2,分解体积庞大的类文件 3,把framework的私有方法公开 分类的特点...

  • iOS知识基础篇

    category 和 extension 的区别 分类有名字,类扩展没有分类名字,是一种特殊的分类 分类只能扩展方...

  • IOS知识基础篇

    category 和 extension 的区别 分类有名字,类扩展没有分类名字,是一种特殊的分类 分类只能扩展方...

  • 1. iOS面试题基础篇

    category 和 extension 的区别 分类有名字,类扩展没有分类名字,是一种特殊的分类 分类只能扩展方...

  • iOS面试题-基础篇

    category 和 extension 的区别 分类有名字,类扩展没有分类名字,是一种特殊的分类 分类只能扩展方...

  • iOS面试题--基础篇

    category 和 extension 的区别 分类有名字,类扩展没有分类名字,是一种特殊的分类 分类只能扩展方...

  • 一些关键字的使用__weak,__block

    1、category 和 extension 的区别分类有名字,类扩展没有分类名字,是一种特殊的分类分类只能扩展方...

网友评论

      本文标题:分类和扩展

      本文链接:https://www.haomeiwen.com/subject/lggmyktx.html