iOS學(xué)習(xí)筆記 - 變量 屬性 方法 實(shí)現(xiàn)
1、代碼說明:
Person.h
Person.h
#import
@interface Person : NSObject
{
int age,sex;//變量的定義
int height,width;
}
@property int age,sex;//屬性的定義
@property char height;
//-(void) setAge;
-(int) setAge1 :(int)a;
-(int) setWH :(int)w :(int)h;
/* 方法的定義
格式
-(返回的數(shù)據(jù)類型) 方法名稱 :(參數(shù)1的數(shù)據(jù)類型)參數(shù)1名稱 :(參數(shù)2的數(shù)據(jù)類型)參數(shù)2名稱
*/
@end
Person.m
Person.m
#import "Person.h"
@implementation Person
@synthesize age,sex;//訪問器
//@synthesize height;
/*
【我的注解】
@synthesize 引用 @property 關(guān)聯(lián) @interface
引用不到,或者關(guān)聯(lián)不到,均會(huì)拋錯(cuò)。
*/
#pragma mark ------setAge----
//-(void) setAge;
//{
// age=20;
//}
#pragma mark ------setAge1------
-(int) setAge1 :(int)a
{
age=a;
return age;
}
#pragma mark ------setWH------
-(int) setWH :(int)w :(int)h //方法的實(shí)現(xiàn)
{
width = 100;
height=175;
return age*height;
}
@end
main.m
main.m
#import
#import "Person.h"
int main(int argc, const char * argv[])
{
@autoreleasepool {
Person *person=[Person alloc];
[person init];
person.age=1;//屬性
NSLog(@"person.ag = %i",person.age);//輸出屬性,注意類型匹配,否則拋錯(cuò)
NSLog(@"person = %@",person);//輸出對(duì)象
[person setWH:6 :10];//方法
[person release];//如果使用了ARC機(jī)制,release就不能用了。
}
return 0;
}
2、我的注解(詳見下面三張圖):
@synthesize 引用 @property 關(guān)聯(lián) @interface
引用不到,或者關(guān)聯(lián)不到,均會(huì)拋錯(cuò)。


【編輯推薦】
- Dropbox遵守蘋果規(guī)定 修復(fù)iOS應(yīng)用被拒問題
- 為什么iOS應(yīng)用比Android應(yīng)用更好看
- S獨(dú)立開發(fā)者:沒有神話,只有故事