IOS之Objective-C学习笔记(一)

来源:互联网 发布:jquery清空表单数据 编辑:程序博客网 时间:2024/04/26 07:12

 当我们创建好一个IOS 程序的时候会看到(如何创建略)

 

  .h 与 .m 文件 也许大家刚接触 会有些疑问到底是干什么用的那?


.h文件  类的声明文件,用于声明类的变量(成员变量)'、 函数方法 @interface --@end

.m文件  类的实现 使用关键字@implementation   ---  @end


--------------分析-----------

以Student.h 及 Student.m为例


//用到某个类 记得要导入进来

//导入类库的时候有<>  与 " "区别在于 一个是引入系统自带类库   另一个是导入自己创建的文件

#import<Foundation/Foundation.h>

声明一个类

@interface Student  : NSObject {//声明一个类  :(代表继承) NSObject

   //这里定义成员变量

    int age;

}

//静态方法 与动态方法区别在于

//--代表动态方法   + 代表静态方法

@end     // 声明类的结束


//age 的get方法

-(int)getAge;

-(void)setAge:(int)_age;



-----------------------------

//导入文件

#import"Student.h"

@implement Student

 -(void)getAge

{

 return age;

}

-(void)setAge:(ing)_age

{

  age = _age;

}

@end //一一对应

//调用一个静态方法alloc来分配内存,OC 使用指针

Student * stu = [[Student alloc] init]]; //调用这个值得init的方法

//最后要释放对象 ,创建一次 ,要释放一次 要一一对应;

[stu release];



0 0