例题:设置个人信息

来源:互联网 发布:excel 提取相同数据 编辑:程序博客网 时间:2024/06/05 16:04

Person.h

#import <Foundation/Foundation.h>@interface Person : NSObject {    NSString *_name;    //名字    NSUInteger _age;    //年龄    float _weight;      //体重}//设置器(set方法)- (void)setName:(NSString *)name;- (void)setAge:(NSUInteger)age;- (void)setWeight:(float)weight;//设置个人信息的方法//方法名:setName: withAge: withWeight:- (void)setName:(NSString *)name        withAge:(NSUInteger)age     withWeight:(float)weight;//访问器(get方法)- (NSString *)name;- (NSUInteger)age;- (float)weight;

Person.m

#import "Person.h"@implementation Person//设置器(set方法)- (void)setName:(NSString *)name {   _name = name;    }- (void)setAge:(NSUInteger)age {    _age = age;}- (void)setWeight:(float)weight {    _weight = weight;    }- (void)setName:(NSString *)name        withAge:(NSUInteger)age     withWeight:(float)weight {    _name = name;    _age = age;    _weight = weight;    }//访问器(get方法)- (NSString *)name {    return _name;    }- (NSUInteger)age {    return _age;}- (float)weight {    return _weight;}

Main.m

#import <Foundation/Foundation.h>#import "Person.h"/*    创建一个人的类(Person),这个人有名字、年龄(int)、体重(float),        并且可以设置和获取人的以上属性。 */int main(int argc, const char * argv[]){   //创建对象person    Person *person = [[Person alloc] init];    //设置器//    [person setName:@"Jack"];//    [person setAge:23];//    [person setWeight:78.4];    //等价于    [person setName:@"Jack" withAge:23 withWeight:78.4];        //访问器    NSString *name = [person name];    NSUInteger age = [person age];    float weight = [person weight];        //打印个人信息    NSLog(@"个人信息:name:%@   age:%ld  weight:%.2f",name,age,weight);


0 0
原创粉丝点击