person.h
#import <Foundation/Foundation.h>
@interface Person : NSObject
@property int age;
@property NSString *name;
@end
person.m
#import "Person.h"
@implementation Person
// 决定了实例对象的输出结果
//- (NSString *)description
//{
// // 下面代码会引发死循环
// // NSLog(@"%@", self);
// return [NSString stringWithFormat:@"age=%d, name=%@", _age, _name];
// //return @"3424324";
//}
// 决定了类对象的输出结果
+ (NSString *)description
{
return @"Abc";
}
@end
main函数
#import <Foundation/Foundation.h>
#import "Person.h"
void test9()
{
// 输出当前函数名
NSLog(@"%s\n", __func__);
}
int main()
{
// 输出行号
NSLog(@"%d", __LINE__);
// NSLog输出C语言字符串的时候,不能有中文
// NSLog(@"%s", __FILE__);
// 输出源文件的名称
printf("%s\n", __FILE__);
test9();
Person *p = [[Person alloc] init];
// 指针变量的地址
NSLog(@"%p", &p);
// 对象的地址
NSLog(@"%p", p);
// <类名:对象地址>
NSLog(@"%@", p);
return 0;
}
void test2()
{
Class c = [Person class];
// 1.会调用类的+description方法
// 2.拿到+description方法的返回值(NSString *)显示到屏幕上
NSLog(@"%@", c);
}
void test1()
{
Person *p = [[Person alloc] init];
p.age = 20;
p.name = @"Jack";
// 默认情况下,利用NSLog和%@输出对象时,结果是:<类名:内存地址>
// 1.会调用对象p的-description方法
// 2.拿到-description方法的返回值(NSString *)显示到屏幕上
// 3.-description方法默认返回的是“类名+内存地址”
NSLog(@"%@", p);
//Person *p2 = [[Person alloc] init];
//NSLog(@"%@", p2);
//NSString *name = @"Rose";
//NSLog(@"我的名字是%@", name);
Person *p2 = [[Person alloc] init];
p2.age = 25;
p2.name = @"Jake";
NSLog(@"%@", p2);
}