现在的位置: 首页 > 综合 > 正文

类的声明和实现

2018年05月26日 ⁄ 综合 ⁄ 共 4051字 ⁄ 字号 评论关闭

用OC完整的写一个函数,需要函数的声明和定义(实现)

用OC完整的写一个类,需要类的声明和实现

类的声明和实现:

#import <Foundation/Foundation.h>
<<1.类的声明:>>
//NSObject 目的是: 让Car这个类具备创建对象的能力
@interface Car : NSObject //加一个类名
 {//用来声明对象属性
	//@public 可以让外部的指针间接访问对象内部的成员变量
	@public  //只需声明一次
	int wheels;//轮胎个数
	int speed;//时速
}
//方法(行为)
//只要是OC对象的方法,必须以减号 - 开头
//OC方法中任何数据类型都必须用小括号()括住,小括号()就这一个作用
 - (void)run;//不写在大括号{}里面

@end  //类的声明完毕


<<2.类的实现:>>
//用来实现interface中声明的方法
@implementation Car
//方法的实现(类的实现)
 - (void)run{
 NSLog(@"");
 }
@end



int main()
{
	//在OC中,想执行一些行为,就要写上一个中括号[ 行为执行着  行为名称]
	Car *p=[Car   new] ;//执行了Car这个类的new行为来创建新的对象
	Car *p2=[Car   new] ;
	p->wheels=4;
	p->speed=250;
	[p2,run];
	p2->wheels=5;
	p2->speed=300; 
	//给p所指向对象发送一条run消息
	[p,run];
	
	NSLog(@"车子有%d个轮子,时速%d",p->wheels,p2->speed);
	
	return 0;
}

OC修改数据必须使用指针

Car *p =[Car new];//定义一个指针变量p,p将来指向的是Car类型的函数,p存储[Car new]返回的地址

OC对象的本质就是结构体

p->wheels=4;//给p所指对象的wheels属性赋值

(instance variable)实例变量=成员变量

@public 可以让外部的指针间接访问对象内部的成员变量,只需创建一次

[Car new] 没出现一次就创建一个新对象,新地址

第二个实例:

#import <Foundation/Foundation.h>

@interface Person:NSObject
{
	int age;
	double weight;
}
- (void)walk;
- (void)ead;
@end

@implementation Person
//实现@interface中声明的方法
- (void)walk{
//对象访问这个方法,则可以直接使用成员变量
	NSLog(@"%d岁,%f公斤的人走了一段路",age,weight);
}
-(void)eat{
	NSLog(@"@"%d岁,%f公斤的人在吃东西",age,weight");
}
@end

int main(){
	Person *p=[Person new];
	p->age=20;
	p->weight=50.0;
	
	[p walk];//若有成员变量age没有赋值,则默认为0
	
	Person *p2=[Person new];
	p2->age=30;
	p2->weight=60.0;
	
	[p2 walk];
	
	Person *p3=p;
	p3->age=33;//会改变第一个p的age值
	
	[p eat];
	
	return 0;
}

OC对象和函数的关系:

OC对象和函数的关系:

#import <Foundation/Foundation.h>
@interface Car : NSObject
{//成员变量
	@public
	int wheels;
	int speed;
}
- (viod)run;

@end

@implemention Car
- (void)run
{
	NSLog(@"%d个轮子,速度为%dkm/h的车子跑起来",wheels,speed);
}
@end

void test(int w,int s)
{
	w=20;
	s=200;
}
void test1(Car *newc)
{
	newc->wheels=5;
}
void test2(Car *newc)
{
	Car *c2=[Car new];
	c2->wheels=5;
	c2->speed=300;
	
	newc=c2;//newc指向了c2的地址空间,所以不会改变调用这个方法的指针所指Car对象的值
	newc->wheels=6
}

int main()
{
	Car *c = [Car new];
	c-wheels=4;
	c->speed=250;
	
	//test(c->wheels,c->speed);
	//[c,run];//4,250
	
	test1(c);
	//[c,run];//5,250
	
	test2(c);
	//[c,run];//4,250  
	
	return 0;
}

若想写多个类:

@interface Car : NSObject
{
	int wheels;//只允许类型+变量名
	int wheels=4;//不允许赋值,只能声明成员变量;不可以重复写;
	static int wheels;//不可以
}
@end
@implementation Car
-(void)run{//没有内部方法的概念,方法是方法,函数是函数
	NSLog(@"");
}
@end

@interface Person : NSObject
{
	int age;
}
@end
@implementation Person
-(void)walk{
	NSLog(@"");
}
@end


类的声明必须放前面,类的实现可以放后面


函数和 方法的区别:

1.方法的声明只能写在@interface 和 @end中间

2.方法的实现只能写在@implementation 和 @end中间

3.对象方法归类/对象所有

4.run()是调用函数的形式 [c,run]是调用方法的形式

5.函数可以写在任何地方,它属于文件所有,不依赖于任何对象,包括@implementation
内,但是在@interface中声明可能有问题。

@implementation Car
void test()
{
NSLog("@调用了test函数");
}
- (void)run
{
test();
NSLog(“@调用了run方法”);
}
@end

类的合理设计:

/*学生:
	成员变量:性别,生日,体重,颜色,狗(体重,毛色,吃,跑)
	方法:吃,跑步,遛狗(让狗跑),喂狗(让狗吃)
*/
#import <Foundation/Foundation.h>
typedef enum
{
	SexMan,
	SexWoman//以枚举名称开头+枚举常量
} Sex;
typedef struct
{
	int year;
	int month;
	int day;
} Date;
typedef enum
{
	ColorBlack,
	ColorRed,
	ColorGreen
} Color;
//1.声明并实现Dog类
@interface Dog : NSObject
{
	@public
	double weight;
	Color curColor;
}
- (void)eat;//狗吃
- (void)run;//狗跑
@end
@implementation Dog
- (void)eat//狗吃
{
	weight+=1;//每吃一次,体重加1
	NSLog(@"狗吃完后,体重=%fkg",weight);
}
- (void)run//狗跑
{
	weight-=1;
	NSLog(@"狗跑完后,体重为=%fkg",weight);
}
@end
//2.声明并实现Student类
@interface Student : NSObject
{
	@public
	char *name;
	//BOOL sex;//不清楚
	Sex sex;//性别
	Date birthday;//生日
	double weight;//体重(kg)
	Color favColor;//颜色
	Dog *dog;
}
- (void)eat;//学生吃
- (void)run;//学生跑
- (void)liuDog;//学生遛狗
- (void)weiDog;//学生喂狗
- (void)print;//打印学生的所有方法
@end

@implementation Student
- (void)eat//学生吃
{
	weight+=1;//每吃一次,体重加1
	NSLog(@"学生吃完后,体重=%fkg",weight);
}
- (void)run//学生跑
{
	weight-=1;
	NSLog(@"学生跑完后,体重为=%fkg",weight);
}
- (void)liuDog//学生遛狗
{
	//让狗跑起来
	[dog run];
}
- (void)weiDog//学生喂狗
{
	//让狗吃东西
	[dog eat];
}
- (void)print
{
	NSLog(@"性别=%d,喜欢的颜色=%d,姓名=%s,生日=%d-%d-%d",sex,favColor,name,birthday.year,birthday.month,birthday.day);
}
@end

//3.主函数
int main()
{
	Student *s=[Student new];
		
	s->weight=50;//体重
	s->name="jack";//名字
	s->sex=SexMan;//性别
	//s->birthday={2011,9,10};//这样是错误的
	/*
		//第一种写法:
		s->birthday.year=2011;//结构体内部成员用点
		s->birthday.month=9;
		s->birthday.day=10;
		
		//第二种写法:
		Data d=s->birthday;
		d.year=2011;
		d.month=2011;
		d.day=2011;
		s->birthday=d;
	*/
	
	//第三种写法:
	Data d={2011,9,10};//生日  在定义结构体变量的同时才是这样的写法
	s->birthday=d;
	
	//s->dog=[Dog new];
	Dog d=[Dog new];//学生的狗
	d->weight=20;
	d->curColor=ColorGreen;
	s->dog=d;
	
	[s liuDog];
	[s weiDog];
	
	[s,eat];
	[s,eat];
	
	[s,run];
	[s,run];
	
	[s print];
	
	return 0;
}

抱歉!评论已关闭.