您好,登錄后才能下訂單哦!
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;
}
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。