美文网首页
NSObject、深拷贝、类对象

NSObject、深拷贝、类对象

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

1.1是所有自定义类的根类
1.2创建对象的方法
alloc、init、dealloc、new
[[TRDog alloc] init];
[TRDog new];
1.3copy:用于实现深拷贝
1.3.1在自定义类的.h文件中,采纳NSCopying协议
1.3.2在自定义类的.m文件中,实现协议中声明的copyWithZone方法
1.3.3在程序中需要深拷贝的地方,用copy方法实现深拷贝,该方法会在函数体中自动调用copyWithZone
1.3.4copy可以做property的参数,使属性赋值是直接得到赋值对象的副本
1.4类对象
1.4.1类对象不是类的对象
1.4.2类对象是一种新的数据类型的数据
1.4.3新的数据类型是Class
1.4.4该数据可用对象(或类名)调用处理class方法获得
1.4.5类对象这种数据类型的数据用于唯一标识一个类
1.4.6类信息比较
1.4.6.1 isKindOfClass:判断某对象是否为指定类(或其父类)的对象
1.4.6.2 isMemberOfClass:判断某对象是否为指定类的对象
1.4.6.2 isSubclassOfClass:判断一个类是否为指定类的子类

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    TRStudent *stu = [TRStudent studentWithName:@"张三" andAge:18];//stu是类TRStudent的对象
    Class c1 = [stu class];//c1是TRStudent的类对象
    Class c2 = [TRStudent class];
    if(c1 == c2)
    {
        self.outputLabel.text = @"对象stu是类TRStudent的对象";
    }
    if([stu class] == [NSString class])
    {
        self.outputLabel.text = @"对象stu是类NSString的对象";
    }
}

每日一练


每日一练(深拷贝).png

TRStudent.h

@interface TRStudent : NSObject<NSCopying>
@property NSString *name;
@property int age;
@property BOOL gender;
@property NSString *address;
-(instancetype)initWithName:(NSString*)name andAge:(int)age andGender:(BOOL)gender andAddress:(NSString*)address;
+(id)studentWithName:(NSString*)name andAge:(int)age andGender:(BOOL)gender andAddress:(NSString*)address;
@end

TRStudent.m

@implementation TRStudent
-(instancetype)initWithName:(NSString *)name andAge:(int)age andGender:(BOOL)gender andAddress:(NSString *)address
{
    self = [super init];
    if(self)
    {
        self.name = name;
        self.age = age;
        self.gender = gender;
        self.address = address;
    }
    return self;
}
+(id)studentWithName:(NSString *)name andAge:(int)age andGender:(BOOL)gender andAddress:(NSString *)address
{
    __autoreleasing TRStudent *stu = [[TRStudent alloc]initWithName:name andAge:age andGender:gender andAddress:address];
    return stu;
}
-(NSString *)description
{
    return [NSString stringWithFormat:@"姓名:%@\n年龄:%d\n性别:%@\n家庭地址:%@",self.name,self.age,self.gender ? @"男" :@"女",self.address];
}
-(id)copyWithZone:(NSZone *)zone
{
    TRStudent *stu = [[TRStudent allocWithZone:zone]initWithName:self.name andAge:self.age andGender:self.gender andAddress:self.address];
    return stu;
}
@end

TRTeacher.h

@interface TRTeacher : NSObject
@property(copy) TRStudent *s;
@property NSString *course;
-(instancetype)initWithS:(TRStudent *)s andCourse:(NSString*)course;
+(id)teacherWithS:(TRStudent *)s andCourse:(NSString*)course;
@end

TRTeacher.m

@implementation TRTeacher
-(instancetype)initWithS:(TRStudent *)s andCourse:(NSString *)course
{
    self = [super init];
    if(self)
    {
        self.s = s;
        self.course = course;
    }
    return self;
}
+(id)teacherWithS:(TRStudent *)s andCourse:(NSString *)course
{
    __autoreleasing TRTeacher *t = [[TRTeacher alloc]initWithS:s andCourse:course];
    return t;
}
-(NSString *)description
{
    return [NSString stringWithFormat:@"所教学生信息\n%@所学的课程是:%@",self.s,self.course];
}
@end

ViewController.m

#import "ViewController.h"
#import "TRStudent.h"
#import "TRTeacher.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UILabel *outputLabel;
-(void)print:(id)obj;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    TRStudent *stu1 = [TRStudent studentWithName:@"郭襄" andAge:29 andGender:NO andAddress:@"四川成都"];
    TRStudent *stu2 = [stu1 copy];
    self.outputLabel.text = [NSString stringWithFormat:@"%@",stu2];
    self.outputLabel.text = [NSString stringWithFormat:@"%p\n%p",stu1,stu2];
    [self print:stu1];
    TRStudent *stu3 = [TRStudent studentWithName:@"杨敏" andAge:53 andGender:NO andAddress:@"四川南充"];
    TRStudent *stu5 = [stu3 copy];
    [self print:stu5];
    stu1.address = @"福建厦门";
    TRTeacher *t = [TRTeacher teacherWithS:stu1 andCourse:@"体育"];
    [self print:t];
}
-(void)print:(id)obj
{
    self.outputLabel.text = [NSString stringWithFormat:@"%@",obj];
    
}
@end

相关文章

网友评论

      本文标题:NSObject、深拷贝、类对象

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