IOS之学习笔记三(简单对象和static和单例)

举报
chenyu 发表于 2021/07/27 01:17:13 2021/07/27
【摘要】 1、Person.h #import <Foundation/Foundation.h> @interface Person : NSObject{ NSString *_name; int _age;} -(void)setName:(NSString *) name andAge:(int) age;-(void)say:(NSString *)conte...

1、Person.h


  
  1. #import <Foundation/Foundation.h>
  2. @interface Person : NSObject
  3. {
  4. NSString *_name;
  5. int _age;
  6. }
  7. -(void)setName:(NSString *) name andAge:(int) age;
  8. -(void)say:(NSString *)content;
  9. -(NSString *)info;
  10. +(void)foo;
  11. @end


Person.m

 


  
  1. #import "Person.h"
  2. @implementation Persion
  3. {
  4. int test;
  5. }
  6. -(void)say:(NSString *)content
  7. {
  8. NSLog(@"%@", content);
  9. }
  10. -(NSString *)info
  11. {
  12. [self test];
  13. return [NSString stringWithFormat:@"名字:%d, 年龄%d,", _name, _age];
  14. }
  15. +(void)foo
  16. {
  17. NSLog(@"通过类名调用");
  18. }
  19. -(void)test
  20. {
  21. NSLog(@"this is a test method");
  22. }
  23. @end
  24. -(void)setName:(NSString *) _name andAge:(int) _age
  25. {
  26. //记得这里是self->_name不是self._name,一定要注意。
  27. self->_name = _name;
  28. self->_age = _age;
  29. }
  30. Person *person = [[Person alloc] init];
  31. [person say:@"hello"];
  32. [person setName:@"chenyu" andAge:26];
  33. NSString *info = [person info];
  34. NSLog(@"info is %@", info);
  35. [Persion foo];

 

2、id类型可以代表所有对象的类型,可以任何类的对象赋值给id类型变量

 


  
  1. id p = [[Person alloc] init];
  2. [p say:@"hello"];

 

3、oc没有类变量,但是可以通过内部全局变量来模拟类变量
oc也提供了static关键字,但是static不能用于修饰成员变量,只能修饰局部变量,全局变量和函数,static修饰局部变量表示将该局部变量存储在静态存储区,static修饰全局变量用于限制全局变量只能在当前源文件中访问,static修饰函数用于限制函数只能在当前文件中调用

模拟类变量

User.h文件如下


  
  1. #import <Foundation/Foundation.h>
  2. @interface User : NSObject
  3. +(NSString *)nation;
  4. +(void)setNation:(NSString *)newNation;
  5. @end

 

User.m文件如下

 


  
  1. #import "User.h"
  2. @implement User
  3. static NSString *nation = nil;
  4. +(NSString *)nation
  5. {
  6. return nation;
  7. }
  8. +(void)setNation:(NSString *)newNation
  9. {
  10. nation = newNation;
  11. }
  12. @end
  13. int main(int argc, char* argc[])
  14. {
  15. @autoreleasepool {
  16. [User setNation:@"chenyu"];
  17. NSLog(@"nation is %@", [User nation]);
  18. }
  19. }

 

 

4、单例模式

Singleton.h文件如下


  
  1. #import <Foundation/Foundation.h>
  2. @interface Singleton : NSObject
  3. +(id)instance;
  4. @end

Singleton.m文件如下


  
  1. @implemnet Singleton
  2. static id instance = nil;
  3. +(id)instance
  4. {
  5. if (instance)
  6. {
  7. instance = [[super malloc] init];
  8. }
  9. return instance;
  10. }
  11. @end
  12. int main(int argc, char* argc[])
  13. {
  14. @autoreleasepool {
  15. NSLog(@"%d", [Singleton instance] == [Singleton instance]);
  16. }
  17. }

 

文章来源: chenyu.blog.csdn.net,作者:chen.yu,版权归原作者所有,如需转载,请联系作者。

原文链接:chenyu.blog.csdn.net/article/details/80891100

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。