OC学习---description方法重写

来源:互联网 发布:声音美化软件app 编辑:程序博客网 时间:2024/04/29 15:33
description方法的作用是打印对象,跟java里的toString有点类似。
对于一个Person类,如果没有重写description方法,NSLog(@%@,p),输出的是 Person:地址,而我们想要的效果是打印出Person的成员变量,所以我们可以在Person类里重写description方法。description方法,返回值是OC字符串


重写实例方法
NSString的字符串拼接用的是stringWithFormat
  1. - (NSString *)description{
  2. Return [NSString stringWithFormat:@”name is%@”,_name];
  3. }
复制代码

+ 重写类方法
类方法:打印类对象
调用类方法:
Class  c=[Person class];
NSLog(@“%@”,c);
0 0
原创粉丝点击