真实的国产乱ⅩXXX66竹夫人,五月香六月婷婷激情综合,亚洲日本VA一区二区三区,亚洲精品一区二区三区麻豆

成都創(chuàng)新互聯(lián)網(wǎng)站制作重慶分公司

OC設(shè)置器與訪問器以及自定義初始化方法-創(chuàng)新互聯(lián)

1.Person.h

成都地區(qū)優(yōu)秀IDC服務(wù)器托管提供商(創(chuàng)新互聯(lián)建站).為客戶提供專業(yè)的成都服務(wù)器托管,四川各地服務(wù)器托管,成都服務(wù)器托管、多線服務(wù)器托管.托管咨詢專線:028-86922220

#import

@interface Person : NSObject

{

  NSString * _name;

  NSString * _sex;

  NSInteger _age;

  CGFloat  _weight;

  CGFloat  _height;

}

- (void)sayhi;

//設(shè)置器  專門給一個實例變量賦值的方法

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

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

- (void)setAge:(NSInteger)age;

- (void)setWeight:(CGFloat)weight;

- (void)setHeight:(CGFloat)height;

//訪問器 用來得到一個實例變量內(nèi)容的方法

- (NSString *)name;

- (NSString *)sex;

- (NSInteger)age;

- (CGFloat)weight;

- (CGFloat)height;

//確定一個方法的方法名

//去掉加減號,去掉返回值類型,去掉形參類型和形參名,剩下的拼在一起就是這個方法的方法名

//OC中沒有方法重載的只要方法名一樣,就認為是同一個方法,不允許重復(fù)定義

+ (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打頭

//自定義初始化方法的作用,創(chuàng)建對象的同時給某些實力變量賦初值

//一個類可以對外提供很多個初始化方法,使用的時候從眾多初始化方法中挑一個使用,初始化方法在對象的生命周期里只使用一次,創(chuàng)建對象的同時使用初始化方法給某些實例變量賦初值.

- (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;

}

//訪問器 用來得到一個實例變量內(nèi)容的方法

- (NSString *)name

{

  return _name;

}

- (NSString *)sex

{

  return _sex;

}

- (NSInteger)age

{

  return _age;

}

- (CGFloat)weight

{

  return _weight;

}

- (CGFloat)height

{

  return _height;

}

//+ 號方法,只能使用類名調(diào)用

//+ 號方法中不能使用實例變量,因為根本就沒有創(chuàng)建對象,只有創(chuàng)建了對象才有內(nèi)存存儲實例變量

+ (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:@"關(guān)羽"];

  [p6 sayhi];[p7 sayhi];

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

  [p8 sayhi];

  return 0;

}

另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機、免備案服務(wù)器”等云主機租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。


當前題目:OC設(shè)置器與訪問器以及自定義初始化方法-創(chuàng)新互聯(lián)
文章源于:http://weahome.cn/article/dpiish.html

其他資訊

在線咨詢

微信咨詢

電話咨詢

028-86922220(工作日)

18980820575(7×24)

提交需求

返回頂部