一、类的声明和实现
1、常见错误
1>.@interface和@implementation不能嵌套包涵
2>.方法只能声明在@interface{}......@end大括号外面
3>.方法的实现只能在@implementation......@end里面
4>.成员变量只能声明在@interface{}......@end大括号里面,切不允许初始化在里面,只能声明
5>.成员变量不可以随便当做C语言里面的变量使用
6>.方法前只可加数据类型名,不可以加static
2、OC方法与函数的区别
1>.对象方法都以-开头
2>.函数是归整个文件所有,与对象一点关系都没有,一般写在文件任意位置,非@interface{}......@end之间
3>.函数内部不能之间调用对象的成员变量
3、类的合理设计
例1、学生
1>.成员变量:性别、生日、体重、最喜欢的颜色、狗(体重、毛色、吃、跑)
2>.方法:吃、跑步、遛狗(跑)、喂狗(吃)
#import<Foudation/Foudation.h>
typedef enum{
SexMan,SexWoman
}Sex;
typedef struct{
int year;
int month;
int day;
}Date;
typedef enum{
ColorBlack,
ColorRed,
ColorGreen
}Color;
@interface Dog : NSobject{
@public
double weight;
Color curColor;
}
-(void)eat;
-(void)run;
@end
@implementation Dog
-(void)eat{
weight+=1;//每吃一次体重+1
NSLog(@"狗吃完这次后的体重是%f",weight);
}
-(void)run{
weight-=1;
NSLog(@"狗跑完这次后的体重是%f",weight);
}
@end
@interface Students : NSobject{
@public
Sex sex;//性别
Date birthday;//生日
double weight;//体重kg
Color favColor;//最喜欢的颜色
char *name;
char *dog;//重点:狗
}
-(void)eat;
-(void)run;
-(void)print;
-(void)liuDog;
-(void)weiDog;
@end
@implementation Students
-(void)eat{
weight+=1;//每吃一次体重+1
NSLog(@"学生吃完这次后的体重是%f",weight);
}
-(void)run{
weight-=1;
NSLog(@"学生跑完这次后的体重是%f",weight);
}
-(void)print{NSLog(@"性别=%d, 喜欢的颜色=%d, 姓名=%s, 生日=%d-%d- %d",sex,favColor,name,birthday.year,birthday.month,birthday.day);
}
-(void)liuDog{
[Dog run]
}
-(void)weiDog{
[Dog eat]
}
@end
int main(){
Students *s=[Students new];
Dog *d=[Dog new];//s->dog=[Dog new]
d->curColor = ColorGreen;
d->weight = 20;
s->dog = d;
s->weight=50;
s->sex=SexMan;//性别
/*
s->birthday.year=2012;//生日
s->birthday.month=2;
s->birthday.day=20;
*/
Date d={2012,2,20};
s->birthday=d;
s->favColor=ColorBlack;
[s eat];
[s eat];
[s run];
[s run];
[s liuDog]
[s weiDog]
return 0;
}
二、方法的声明和实现
例1、/*计算器-方法:返回π、计算摸个整数的平方、计算两个数的和*/
#import<Foudation/Foudation.h>
@interface JiSuanQi:NSobject
//OC中不允许方法名一样,冒号也是方法名的一部分
-(double)pi;//方法名:pi
-(int)pingFang:(int)num;//OC方法中一个参数对应一个冒号,必须写,方法名:pingFang:
//第一种写法-(int)sum:(int)num1 :(int)num2;num1和后面的冒号之间必须加冒号
-(int)sumWithNum1:(int)num1 andNum2:(int)num2;//方法名:sumWithNum1: andNum2:
@end
@implementation JiSuanQi
-(double)pi{
return 3.14;
}
-(int)pingFang:(int)num{
return num * num;
}
//第一种写法:-(int)sum:(int)num1 :(int)num2
-(int)sumWithNum1:(int)num1 andNum2:(int)num2{
return num1+num2;
}
@end
int main(){
JiSuanQi *jsq=[JiSuanQi new];
double a=[jsq pi];
NSLog(@"%f\n",a);
int a=[jsq pingFang:10];//冒号的作用,分割参数的值
//int a=[jsq sum:10 :5];//10和冒号之间必须加空格
int a=[jsq sumWithNum1:10 andNum2:5];
NSLog(@"%i",a);
return 0;
}
练习 给Car类设计一个方法,用来和其他车比较车速,返回车速的差距。
#import<Foudation/Foudation.h>
@interface Car:NSobject{
@public
int speed;
}
-(int)compareSpeedWithOther:(Car *)other;
@end
@implementation Car
-(int)compareSpeed:(Car *)other{
return speed-other->speed;
}
@end
int main(){
Car *c1=[Car new];//Car *c1;c1=[Car new];
c1->speed=300;
Car *c2=[Car new];
c2->speed=500;
int a=[c1 compareSpeedWithOther:c2]//调用时注意谁是Car *、谁是other
NSLog(@"a=%d\n",a);
return 0;
}
2、匿名对象 格式:[Car new]->speed=300;//不要写这样的代码,只要求能说出
结果就行
3、IOS文档安装
1>.直接放到Xcode 中/Applications/Xcode.app/Contents/Developer/Documentatio n/DocSets
2>.放到硬盘的某个路径/Users/用户 名/Library/Developer/Shared/Documentation/DocSets
三、面向对象的三大特性之封装
1、三大特性:封装、继承、多态
1>.封装set方法
#import<Foudation/Foudation.h>
@interface Student:NSobject{
//@public//封装的时候成员变量不可以带@public,尽量使用set方法,保证数据安全性
int age;
}
- (void)study;
@end
@implementation Student
- (void)study{
NSLog(@"%d岁的学生在学习",age);
}
@end
int main(){
Student *stu = [Student new];
stu->age=20;
return 0;
}
2>.set方法作用:提供一个方法给外界设置成员变量的值
3>.命名规范:方法名必须以set开头,set后必须跟上成员变量的名 称,成员变量首字母需大写,返回值一定是void,一定接收一个参数 ,切跟成员变量的类型一致
4>.set方法声明与实现
- (void)setAge:(int)newAge;//声明
- (void)setAge:(int)newAge{//实现
if(newAge <= 0){
newAge = 1;
}
age = newAge;
}
[stu setAge:-10];//调用
5>.get方法
作用:返回对象内部成员变量
命名规范:肯定有返回值,与成员变量类型一致,方法名必须与跟成
员变量名一样、不需要接受任何参数
声明在@interface里面
- (int)age;
实现@implementation里面
- (int)age{
return age;
}
调用在main函数里面
NSLog(@"学生的年龄是%d岁",[stu age]);
只允许外界访问我的no不许改,只需要提供get方法就可以实现
6>.封装的一点细节
成员变量的命名规范:成员变量的命名一定要以下划线_开头
作用:让成员变量和get方法的名称区分开,也可以跟局部变量区分
开,一看到下划线开头的变量,一般都是成员变量,例:
#import<Foudation/Foudation.h>
typedef enum{//定义一个枚举类型
SexMan,
SexWoman
} Sex;
@interface Student:NSobject{//声明成员变量
int _no;
Sex _sex
}
//声明no的set方法和get方法
- (void)setNo:(int)no;
-(int)no;
//声明sex的set方法和get方法
- (void)setSex:(Sex)sex;
-(Sex)sex;
@end
@implementation Student
- (void)setNo:(int)no{//set方法
_no=no;
}
-(int)no{//get方法
return _no;
}
- (void)setSex:(Sex)sex{//set方法
_sex=sex;
}
-(Sex)sex{//get方法
return _sex
}
@end