description方法

来源:互联网 发布:数据情报 编辑:程序博客网 时间:2024/05/02 05:04

OC中的description 方法和Java toString 方法类似,都是用来对对象实行串行化输出


@interface Student : NSObject@property(nonatomic)NSString *name;@end@implementation Student@end

这段代码简单的自定义了一个类

如果对该类的一个对象用NSLog输出

    Student *student=[[Student alloc]init];    student.name=@"zhangsan";    NSLog(@"%@",student);

结果如下

<Student: 0x100100320>
在自定义类中覆盖description方法

@interface Student : NSObject@property(nonatomic)NSString *name;-(NSString *)description;@end@implementation Student-(NSString *)description{    return self.name;}@end
输出结果

zhansan

description方法是NSObject类中定义的,覆盖继承的description方法可以实现格式化字符显示类对象,在java中toString方法功能相同。


0 0
原创粉丝点击