033.description 自我描述的方法
来源:互联网 发布:sql自定义函数 编辑:程序博客网 时间:2024/04/28 12:21
--------------- main.m ---------------
#import<Foundation/Foundation.h>
@interfaceFKApple : NSObject
@property(nonatomic, copy) NSString* color;
@property(nonatomic, assign) double weight;
- (id) initWithColor: (NSString*) color weight: (double) weight;
@property(nonatomic, copy) NSString* color;
@property(nonatomic, assign) double weight;
- (id) initWithColor: (NSString*) color weight: (double) weight;
@end
@implementationFKApple
- (id) initWithColor: (NSString*) color weight:(double) weight
{
if(self= [superinit])
{
self.color = color;
self.weight = weight;
}
return self;
}
- (id) initWithColor: (NSString*) color weight:(double) weight
{
if(self= [superinit])
{
self.color = color;
self.weight = weight;
}
return self;
}
- (NSString*) description
{
return[NSString stringWithFormat:@"<FKApple[_color=%@, _weight=%g]>"
, self.color , self.weight];
}
, self.color , self.weight];
}
@end
intmain()
{
FKApple* a = [[FKApple alloc] initWithColor:@“红色" weight:5.68];
NSLog(@"%@", a);
}
一、编写本节代码的具体步骤:
1.可仿照第二章001节的代码编写步骤,可以把类的接口文件,类的实现文件写在main.m文件中。
二、本节代码涉及到的知识点:
1.当我们使用NSLog函数输出一个对象的时候,输出的就是该对象的description方法的返回值。
2.description方法是NSObject类的一个方法,也就是说,所有的OC对象都具有description方法。
3.description方法默认会返回一个内存地址,如果需要更多“自我描述”,我们还得自己去重写该方法。
0 0
- 033.description 自我描述的方法
- 033.description 自我描述的方法
- Description描述性编程方法:
- DedeCms中文章描述description标签限制字数的方法
- NSObject的描述(+description)方法和对象的描述(-description)方法,NSLog默认输出的修改(后知后觉)
- 自我的描述
- OC的description方法
- 获取组SPGroup的描述Description信息
- 获取枚举类型的描述description
- 分词研究中的最小描述长度(Minimum description length)方法
- IOS开发--Description描述方法书写小技巧
- qtp学习-用三种方式登录-录制、直接描述、Description方法
- iOS: NSData的方法description
- iOS: NSDictionary的方法description
- iOS: NSArray的方法description
- Objective-C的description方法
- OC description方法的使用
- Objective-C的description方法
- 029.Polymorphism 多态
- 030.(type*)variable 指针的强制类型转换
- 单调栈经典模板(Codeforces Round #305 (Div. 2), problem: (D) Mike and Feet)
- 031.isKind 判断指针的实际类型
- 032.NSValue NSNumber 包装类
- 033.description 自我描述的方法
- 034.== 两个变量是否相等
- 035.isEqual 两个变量是否相等
- 036.isEqual 不合理的重写
- 037.isEqual 合理的重写
- #leetcode#Simplify Path
- JQuery.validate.js实现Ajax验证
- C++面向对象编程(六) 继承
- 2015.07.24总结