#import <Foundation/Foundation.h>
@interface Person : NSObject
@property (nonatomic, retain) NSString *name;
@property (nonatomic, assign) int age;
- (void)print; // 对象方法
+ (void)introduce; // 类方法
@end
@implementation Person
- (void)print{
// 以下方式均可以输出对象的属性
NSLog(@"name = %@, age = %d", self.name, self.age);
NSLog(@"name = %@, age = %d", _name, _age);
NSLog(@"name = %@, age = %d", [self name], [self age]);
}
+ (void)introduce{
NSLog(@"欢迎来到 以神之名 的博客园!");
}
@end
int main(int argc, const char * argv[]){
Person *p = [[Person alloc] init];
Person *p2 = [[Person alloc] init];
[p setName:@"Jerry"];
[p setAge:10];
p2.name = @"Tom";
p2.age = 12;
[p print];
[p2 print];
[Person introduce];
return 0;
}
时间: 2024-10-08 00:26:26