1、Person.h
#import <Foundation/Foundation.h>
@interface Person : NSObject
{
NSString *_name;
int _age;
}
-(void)setName:(NSString *) name andAge:(int) age;
-(void)say:(NSString *)content;
-(NSString *)info;
+(void)foo;
@end
Person.m
#import "Person.h"
@implementation Persion
{
int test;
}
-(void)say:(NSString *)content
{
NSLog(@"%@", content);
}
-(NSString *)info
{
[self test];
return [NSString stringWithFormat:@"名字:%d, 年龄%d,", _name, _age];
}
+(void)foo
{
NSLog(@"通过类名调用");
}
-(void)test
{
NSLog(@"this is a test method");
}
@end
-(void)setName:(NSString *) _name andAge:(int) _age
{
//记得这里是self->_name不是self._name,一定要注意。
self->_name = _name;
self->_age = _age;
}
Person *person = [[Person alloc] init];
[person say:@"hello"];
[person setName:@"chenyu" andAge:26];
NSString *info = [person info];
NSLog(@"info is %@", info);
[Persion foo];
IOS之学习笔记三(简单对象和static和单例)
最新推荐文章于 2024-08-24 17:22:53 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)