您的位置:首页 > 移动开发 > IOS开发

iOS注意事项

2015-07-27 22:00 471 查看
1.封装,继承,多态
Set:方法1.作用提供一个方法给外界设置成员变量的
命名规范
作用:提供一个变量方法给外界设置成员变量值,可以在方法里面对参数进行相应的过

set后面跟上成员变量的名称,成员变量的首字母必须大写

一定要接收一个参数,而且参数类型跟成员变量类型一致

形参的名称不能跟成员变量名一样
Get:方法

返回对象内部的成员变量

肯定有返回值,返回值类型肯定和成员变量类型一致,方法名跟成员变量名一致,不需要接 受任何参数
#import <Fountation/Fountation.h>
@interfceStudent : NSObject
{
Int age;
}
-(void)setAge:(int)newAge;
@end
@implementationStudent
-(void)setAge:(int )newAge{
if (newAge<= 0)
{
newAge =1;
}
age =newAge;

}
-(void)study
{

NSLog("%d
学生在学习",age);

}
@end
Int main()
{
Student *stu =[Student new];

[stu setAge:0];
[stustudy];
}
2.对于字符一应该用单引号扩起来'Z'

双引号表示一个字符串
3.类方法智能用类进行调用,当类方法用实例调用时,会出现闪退现象导致程序崩溃
4.对象方法不能用类对象调用,也会出现程序崩溃
5.两者的区别,类方法+开头,只能由类对象来调用

对象方法-开头,只能由对象来调用,两者可以重名,对象方法中能调用当前 对象的成员变量

//

// main.m

// yun

//

// Created by qingyun on 15/7/27.
// Copyright (c) 2015年qingyun. All rights reserved.

//

#import
<Foundation/Foundation.h>

@interface Score:NSObject
{

int _cscore;

int _ocscore;

int _totalscore;

int _averagescore;

}
-(void)setCscore:(int)cscore;
-(int) cscore;
-(void)setOcscore:(int)ocscore;
-(int)ocscore;
-(int)totalscore;
-(int)averagescore;

@end
@implementation Score

-(void)setCscore:(int)cscore
{

_cscore =cscore;

_totalscore = _ocscore+_cscore;

_averagescore = _totalscore/2;

NSLog(@"_total value is %d\n",_totalscore);
}
-(int)cscore
{

return _cscore;
}
-(void)setOcscore:(int)ocscore
{

_ocscore=ocscore;
}
-(int)ocscore
{

return _ocscore;
}
-(int)totalscore
{

return _totalscore;
}
-(int)averagescore
{

return _averagescore;
}

@end
int main ()
{

Score *s =[Scorenew];
[s
setOcscore:100];
[s
setCscore:90];
[s
averagescore];

int a = [s totalscore];

NSLog(@"总分:%d",a);

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: