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;
}
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;
}
相关文章推荐
- IOS开发小项目—找色块游戏
- IOS中NSUserDefaults的用法(轻量级本地数据存储) (转)
- iOS开发雕虫小技之傻瓜式定位神器-超简单方式解决iOS后台定时定位
- iOS反射机制
- iOS单例模式(Singleton)
- Xcode 7如何 免费 真机调试iOS应用
- IOS Quartz 各种绘制图形用法---实现画图片、写文字、画线、椭圆、矩形、棱形等
- iOS设计模式 - 桥接
- iOS陀螺仪计算行走的步数
- iOS开发---轮播图模块(普通版)
- iOS开发之*.a静态库注意事项
- IOS的safari浏览器的电话号码识别功能的禁用
- js判断请求对象是IOS还是安卓用户
- iOS 半透明新手引导 手把手教你做
- iOS tableView的刷新 笔记
- iOS OC08,09_内存管理
- iOS学习笔记--(c基础题9)
- ios申请真机调试(Xcode 5)详细解析
- iOS 创建静态库static library
- IOS中的block的retain循环