美文网首页
iOS 类簇

iOS 类簇

作者: 口子窖 | 来源:发表于2021-07-14 14:24 被阅读0次

iOS 类簇

Person.h

#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

@interface Person : NSObject
@property (nonatomic,assign) NSUInteger age;

- (instancetype)personWithAge:(NSUInteger)age;
@end

NS_ASSUME_NONNULL_END

Person.m

#import "Person.h"
@interface __PersonGirl : Person

@end
@implementation __PersonGirl

@end

@interface __PersonWoman : Person

@end
@implementation __PersonWoman

@end


@implementation Person

- (instancetype)personWithAge:(NSUInteger)age
{
    if (age <= 18) {
        __PersonGirl *girl = [[__PersonGirl alloc] init];
        girl.age = age;
        return girl;
    }else {
        __PersonWoman *woman = [[__PersonWoman alloc] init];
        woman.age = age;
        return woman;
    }
}

@end

TestCode

#import "ClassClusterViewController.h"
#import "Person.h"
@interface ClassClusterViewController ()

@end

@implementation ClassClusterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    Person *person1 = [[Person alloc] personWithAge:15];
    Person *person2 = [[Person alloc] personWithAge:30];
    NSLog(@"age:%lu,class:%@",(unsigned long)person1.age,[person1 class]);
    NSLog(@"age:%lu,class:%@",(unsigned long)person2.age,[person2 class]);
}

@end

打印结果
2021-07-14 14:19:16.392992+0800 TestUI[39422:1747234] age:15,class:__PersonGirl
2021-07-14 14:19:16.393149+0800 TestUI[39422:1747234] age:30,class:__PersonWoman

相关文章

  • iOS类簇

    IOS 类簇 类簇(class cluster)是一种设计模式,在Foundation Framework中被广泛...

  • iOS 类簇

    iOS 类簇 Person.h Person.m TestCode 打印结果2021-07-14 14:19:16...

  • iOS 类族(类簇)

    什么是类族 "类族"是一种很有用的模式(pattern),可以隐藏"抽象基类"背后的实现细节.比如UIKit框架中...

  • iOS开发 - 类簇

    因此,直接alloc可能造成程序奔溃.比如比如 NSNotification *notif = [[NSNotif...

  • 类簇

    类簇在iOS中是一种非常实用的模式,例如:NSArray, NSDictionary等。 那么类簇有哪些特点了: ...

  • iOS 中的类簇

    类簇的定义 类簇(Class Cluster)是定义相同的接口并提供相同功能的一组类的集合,仅公开接口的抽象类也...

  • iOS 类簇(class cluster)

    概括 类簇是一种设计模式(抽象工厂模式),它管理了一组隐藏在公共接口下的私有类。 详解 简单来说,我们调用的是父类...

  • 【iOS】类簇(class cluster)

    类簇实际上是Foundation framework框架下的一种设计模式,它管理了一组隐藏在公共接口下的私有类。 ...

  • iOS 类簇(class clusters)

    类簇(class clusters) 类簇是Foundation framework框架下广泛使用的一种设计模式。...

  • 设计模式

    设计模式: 类簇 在iOS的Foundation框架中,类簇是一种常用的设计模式,将一些相近的,私有的,具体的子类...

网友评论

      本文标题:iOS 类簇

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