温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

OC设置器与访问器以及自定义初始化方法

发布时间:2020-07-28 10:12:05 来源:网络 阅读:774 作者:peipeiyi2014 栏目:开发技术

1.Person.h

#import <Foundation/Foundation.h>


@interface Person : NSObject

{

    NSString * _name;

    NSString * _sex;

    NSInteger  _age;

    CGFloat    _weight;

    CGFloat    _height;

}

- (void)sayhi;

//设置器   专门给一个实例变量赋值的方法

- (void)setName:(NSString *)name;

- (void)setSex:(NSString *)sex;

- (void)setAge:(NSInteger)age;

- (void)setWeight:(CGFloat)weight;

- (void)setHeight:(CGFloat)height;


//访问器  用来得到一个实例变量内容的方法

- (NSString *)name;

- (NSString *)sex;

- (NSInteger)age;

- (CGFloat)weight;

- (CGFloat)height;


//确定一个方法的方法名

//去掉加减号,去掉返回值类型,去掉形参类型和形参名,剩下的拼在一起就是这个方法的方法名

//OC中没有方法重载的只要方法名一样,就认为是同一个方法,不允许重复定义

+ (void)testMethd;

- (void)changName:(NSString *)name

              sex:(NSString *)sex

              age:(NSInteger)age

           weight:(CGFloat)weight

           height:(CGFloat)height;

- (void)modifyName:(NSString*)name

               sex:(NSString *)sex

               age:(NSInteger)age

            weight:(CGFloat)weight

            height:(CGFloat)height;


//自定义的初始化方法

//初始化方法都是-号方法,返回值是id,方法名以intwith打头

//自定义初始化方法的作用,创建对象的同时给某些实力变量赋初值

//一个类可以对外提供很多个初始化方法,使用的时候从众多初始化方法中挑一个使用,初始化方法在对象的生命周期里只使用一次,创建对象的同时使用初始化方法给某些实例变量赋初值.

- (id)initwithName:(NSString *)name;

- (id)initwithSex:(NSString *)sex;

- (id)initwithAge:(NSInteger)age;

- (id)initwithWeight:(CGFloat)weight;

- (id)initwithHeight:(CGFloat)height;


- (id)initwithName:(NSString *)name

               sex:(NSString *)sex;

- (id)initwithName:(NSString *)name

               age:(NSInteger)age;

- (id)initwithsex:(NSString *)sex

              age:(NSInteger)age;

- (id)initwithweight:(CGFloat)weight

              height:(CGFloat)height;


- (id)initwithName:(NSString *)name

               sex:(NSString *)sex

               age:(NSInteger)age;

- (id)initwithName:(NSString *)name

               sex:(NSString *)sex

            weight:(CGFloat)weight;


- (id)initwithName:(NSString *)name

               sex:(NSString *)sex

               age:(NSInteger)age

            weight:(CGFloat)weight

            height:(CGFloat)height;


@end

2.Person.m

#import "Person.h"


@implementation Person

- (id)init

{

    _name = @"宝强";

    _sex = @"男";

    _age = 25;

    _weight = 65;

    _height = 166;

    return self;

}

- (void)sayhi

{

    NSLog(@"\n姓名%@,性别%@,年龄%ld,体重%.2f,身高%.2f",_name,_sex, _age,_weight,_height);

}

- (void)setName:(NSString *)name

{

    _name = name;

}


- (void)setSex:(NSString *)sex

{

    _sex = sex;

}

- (void)setAge:(NSInteger)age

{

    _age = age;

}

- (void)setWeight:(CGFloat)weight

{

    _weight = weight;

}

- (void)setHeight:(CGFloat)height

{

    _height = height;

}


//访问器  用来得到一个实例变量内容的方法

- (NSString *)name

{

    return _name;

}

- (NSString *)sex

{

    return _sex;

}

- (NSInteger)age

{

    return _age;

}

- (CGFloat)weight

{

    return _weight;

}

- (CGFloat)height

{

    return _height;

}


//+ 号方法,只能使用类名调用

//+ 号方法中不能使用实例变量,因为根本就没有创建对象,只有创建了对象才有内存存储实例变量

+ (void)testMethd

{

    NSLog(@"hello ");

}

- (void)changName:(NSString *)name sex:(NSString *)sex age:(NSInteger)age weight:(CGFloat)weight height:(CGFloat)height

{

    _name = name;

    _sex = sex;

    _age = age;

    _weight = weight;

    _height = height;

}

- (void)modifyName:(NSString*)name

               sex:(NSString *)sex

               age:(NSInteger)age

            weight:(CGFloat)weight

            height:(CGFloat)height

{

    _name = name;

    _sex = sex;

    _age = age;

    _weight = weight;

    _height = height;

}


//自定义初始化方法

- (id)initwithName:(NSString *)name

{

    _name = name;

    return self;

}

- (id)initwithSex:(NSString *)sex

{

    _sex = sex;

    return self;

}

- (id)initwithAge:(NSInteger)age

{

    _age = age;

    return self;

}

- (id)initwithWeight:(CGFloat)weight

{

    _weight = weight;

    return self;

}

- (id)initwithHeight:(CGFloat)height

{

    _height = height;

    return self;

}


- (id)initwithName:(NSString *)name

               sex:(NSString *)sex

{

    _name = name;

    _sex = sex;

    return self;

}

- (id)initwithName:(NSString *)name

               age:(NSInteger)age

{

    _name = name;

    _age = age;

    return self;

}

- (id)initwithsex:(NSString *)sex

              age:(NSInteger)age

{

    _sex = sex;

    _age = age;

    return self;

}

- (id)initwithweight:(CGFloat)weight

              height:(CGFloat)height

{

    _weight = weight;

    _height = height;

    return self;

    

}


- (id)initwithName:(NSString *)name

               sex:(NSString *)sex

               age:(NSInteger)age

{

    _name = name;

    _sex = sex;

    _age = age;

    return self;

}

- (id)initwithName:(NSString *)name

               sex:(NSString *)sex

            weight:(CGFloat)weight

{

    _name = name;

    _sex = sex;

    _weight = weight;

    return self;

}


- (id)initwithName:(NSString *)name

               sex:(NSString *)sex

               age:(NSInteger)age

            weight:(CGFloat)weight

            height:(CGFloat)height

{

    _name = name;

    _sex = sex;

    _age = age;

    _weight = weight;

    _height = height;

    return self;

}


@end

3.main.m

int main(int argc, const char * argv[]) {

   

   Person * p = [[Person alloc]init];

    [p sayhi];

    Person * p1 = [[Person alloc]init];

    [p1 setName:@"zhangsan"];//[p1 sayhi];

    NSLog(@"%@",[p1 name]);

    Person * p2 = [[Person alloc]init];

    [p2 setName:@"zhaosi"];

    [p2 setSex:@"男"];

    [p2 setAge:20];

    [p2 setWeight:120];

    [p2 setHeight:178];

    NSLog(@"%@,%@,%ld,%.2f斤,%.2fcm",[p2 name],[p2 sex], [p2 age],[p2 weight],[p2 height]);

    Person * p3 = [[Person alloc]init];

    [p3 changName:@"祖蓝" sex:@"男" age:30 weight:30 height:177];

    NSLog(@"%@,%@,%ld,%.2f斤,%.2fcm",[p3 name],[p3 sex], [p3 age],[p3 weight],[p3 height]);

    Person * p4 = [[Person alloc]init];

    [p4 modifyName:@"李晨" sex:@"男" age:30 weight:30 height:177];

    NSLog(@"%@,%@,%ld,%.2f斤,%.2fcm",[p4 name],[p4 sex], [p4 age],[p4 weight],[p4 height]);

    Person * p5 = [[Person alloc]init];

    [p5 sayhi];

 

 // 自定义初始化方法

    Person * p6 = [[Person alloc] initwithName:@"吴强"];

    Person * p7 = [[Person alloc] initwithName:@"赵子龙"];

    [p6 setName:@"关羽"];

    [p6 sayhi];[p7 sayhi];

    Person * p8 = [[Person alloc]initwithName:@"张飞" sex:@"男" age:30 weight:70 height:180];

    [p8 sayhi];

  return 0;

}



向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI