加入收藏 | 设为首页 | 会员中心 | 我要投稿 汽车网 (https://www.0577qiche.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 教程 > 正文

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

发布时间:2023-03-25 11:34:19 所属栏目:教程 来源:
导读:#import <Foundation/Foundation.h>
@interface Person : NSObject
{
Nsstring *_name;
int _age;
}
-(void)setName:(Nsstring *) name andAge:(int) age;
-(void)say:(Nsstring *)content;
-(Nss
#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];
 
id类型可以代表所有对象的类型,可以任何类的对象赋值给id类型变量

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

(编辑:汽车网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章