`
dcj3sjt126com
  • 浏览: 1823075 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

objective-c学习笔记第一天

    博客分类:
  • IOS
阅读更多

主机内容

类的申明

函数/消息的定义方法

字段作用域

 

 

语法纵览

关键字都有@开始,比如:

@class, @interface, @implementation,@public @private, @protected, @try, @catch, @throw, @finally, @end, @protocol, @selector, @synchronized, @encode, @defs

 

基本面向过程

比较项目 OC和C语言对比

基本类型 char, short, int, long long, BOOL

布尔 TRUE, FALSE    YES,NO

基本语句 if else switch for while do goto

for额外扩展 for ( xx in xx ) OC特有

 

面向对象概述

比较项目 OC

基类 NSObject

单继承 是单继承

接口 支持接口(协议) @protocol,接口方法可选实现

多继承 使用接口来实现多继承

多态 支持多态

抽象类 支持抽象类

异常处理 简单的异常处理

虚函数 所有的函数都是虚函数

 

和C/C++一些比较

OC和C/C++一样,都是用如下:

/* ... */ 和 //作为注释

 

BOOL,YES,NO

在C++中,布尔类型是bool, 在OC中,布尔类型是BOOL,布尔值有YES和NO两种

 

ID类型

OC中,每个目标都可以表达为ID类型,范性,可以认为是 NSObject * 或者void *

 

nil

nil等同于 null, 表示一个目标的指针

 

类的定义

OC类分为2个文件,一个是.h,一个是.m文件

.h文件存放类,函数申明

.m文件存放类的具体实现 

 

类申明使用关键字@interface  @end来申明

类实现使用关键字@implementation  @end来实现 

 

类声明   <Dog.h>

#import <Foundation/Foundation.h>

@interface Dog : NSObject {

//此处写字段

}

//此处写函数

 

@end

 

导入  To import or include ?

#import head.h

C/C++使用 #include 来包含头文件。缺点就是可能同一个头文件可能被包含多次

Object-C使用了#import来包含头文件。优点就是同一个头文件只能包含一次。

 #ifndef  __HEAD_H__

 #define  __HEAD_H__

 

 #endif

 

创建/销毁OC对象

创建对象

Dog * dog = [Dog alloc];

 

初始化构造函数

[dog init];

 

销毁对象

[dog release];

 

类中字段和函数

@interface Dog : NSObject {

int age; //字段定义在此

}

 

- (void)  setAge:(int) newAge; //函数定义在此

@end

 

字段定义

C++和OC在变量申明一样,都有public protected private 三种作用域

(C++) public: protected: private:

(OC)  @public @protected @private

OC缺少是@protected

C++和OC在函数声明不一样

OC函数全部都是public类型(用另外方式实现 private化)

变量必须类 {}之间的地方

 

变量作用域申明

文件Dog.h

@interface Dog : NSObject {

@public //可以是@public  @protected @private

int age;

@protected 

int ID;

@private 

float price;

}

@end

 

类申明比较

所有OC类,接口声明必须要带 *

这里*既表示指针也表示引用

 

OC:

Dog * myDog;

 

这里 * 即表示指针(又不表示指针), 也表示引用

可以通过myDog->dog 或者 myDog.dog这些方法来访问

 

一个典型类的实现 

类声明<Dog.h>

#import <Foundation/Foundation.h>

@interface Dog : NSObject {

int ae;

}

 

//下面这些在OC中是消息,但是为了方便理解就叫函数

-(id) init;

-(id) initWithAe:(int)newAge;

-(int) getAge;

-(void) setAge:(int)newAge;

@end

 

Dog.m

#import "Dog.h"

@impolementation Dog

 

-(id)int {

return [self initWithAge:10];

}

 

-(id) initWithAge:(int)newAge{

self = [super init];

if (self) {

age = newAge;

}

return self;

}

 

-(int) getAge {

return age;

}

-(void) setAge:(int) newAge {

age = newAge;

}

 

@end

 

函数定义

在OC中属性是申明只能在@interface{和}之间。

属性的使用和C语言类似

在@interface{和}之间不能定义方法。

方法的定义是:

- (int) f : (int)x;

这里-表示对象的方法,+表示类的方法和C++类似

返回值或者参数的类型申明是使用()包含

参数分割使用:来分开

 

函数定义例子

典型的函数和变量定义

文件Foo.h

@interface Foo : NSObject {

double x;

}

-(int) f:(int) x;

-(float) g :(int)x :(int)y;

@end

 

文件Foo.m

#ipmort "Foo.h"

@implementation Foo

-(int) f:(int) x {...}

-(float) g:(int)x :(int)y {...}

@end

 

多参数情况 

-(int) f:(int)x; 类似于C中的int f(int x);

函数不带参数(函数名:f)

- (int) f

类似C int f()函数

 

带一个参数(函数名: f:x)

- (int) f:(int)x

类似C的 int f(int x)函数

 

带两个参数(函数名:f::)

- (float) f:(int)x :(int)y

类似C的float f(int x,int y);

 

多参数方法

带两个参数(函数名:f::)

- (float) f:(int)x :(int) y

参数能够在:前面设置一个标签label,标签label也是函数名的一部分,标签是为了方便阅读(不带标签实际上也有一个匿名标签)

 

比如上述函数可以改成

- (float) f:(int)x g:(int)y

这里 g表示标签,函数名是f:g:

第一个参数不能有标签,事实上函数名就是第一个参数的标签

 

函数调用对别

比较项目 C OC

无参数 int foo(); - (int) foo;

如何调用的 int ret = obj->foo(); int ret = [obj foo];

 

一个参数 int foo(int a); - (int) foo:(int)a;

如何调用的 int ret = obj->foo(100); int ret = [obj foo:100];

 

二个参数 int foo(int a,int b); -(int)foo:(int)a :(int)b;

如何调用的 int ret = obj->foo(10,20); int ret = [obj foo:10 :20];

 

带标签 int fooAndB(int a, int b); -(int) foo:(int)a andB:(int)b;

如何调用的 int ret = obj->fooAndB(10,2); int ret = [obj foo:10 andB:2];

 

三个参数例子

C例子

int insertObjectAtIndexBefore (Object o, int index, boolean before);

int ret = obj->insertObjectAtIndexBefore(str, 2, true);

 

OC例子

-(int) insertObject:(NSObject *)o AtIndex:(int)index Before:(BOOL)before

int ret = [obj insertObject:10 AtIndex:2 Before:TRUE];

 

多参数的方法调用

-(void) insertObject:(int) value :(unsigned int)index

 返回值 函数名 第一个参数 第二个参数

 第二个参数不带标签label,也就是匿名标签,函数名

 insertObject::

 调用方法:[obj insertObject:100 :20]

[obj insertObject:101 :21]

 

-(void) insertObject:(int)value atIndex:(unsigned int)index

返回值 函数名 第一个参数 标签 第二个参数

第二个参数的标签是atindex。函数名为insertObject:atIndex:

调用方法:[obj insertObject:100 atIndex:20];

[obj insertObject:101 atIndex:21];

 

 

函数重载

@interface Foo : NSObject {}

-(int) g:(int) x;

-(int) g:(float) x; //错误:这个方法和前一个方法冲突(因为没有标签)

-(int) g:(int) x:(int) y; //正确:两个匿名的标签

-(int) g:(int) x:(float) y; //错误:也是两个匿名的标签

-(int) g:(int) x andY:(int) y; //正确:第二个标签是andY

-(int) g:(int) x andY:(float) y; //错误:第二个标签也是andY

-(int) g:(int) x andAlsoY:(int) y; //正确:第二个标签是andAlsoY

@end

 

 

各类函数名

@interface Foo : NSObject{}

//函数名是"g"

-(int)g;

//函数名是"g:"

-(int)g:(float)x;

//函数名是"g::"

-(int)g:(float)x :(float)y;

//函数名是"g:andY:"

-(int)g(float)x andY:(float) y;

//函数名是"g:andZ"

-(int)g:(float) x andZ:(float) z;

@end

 

 

Message消息机制

使用发送目标对象一个消息来达到处理函数

使用如下的格式来出来发送消息

[object message]

object.message

因为是动态绑定,消息和接受者都是在运动时动态绑定在一起。

object表示一个对象或者一个类,

message消息也可以认为是一个函数。

 

函数作用域申明

OC在.h头文件定义的所有函数都是public类型

OC通过Categories来实现函数的私有化

 

文件Dog.h

#import <Foundata/Foundation.h>

 

@interface Dog : NSObject

{//写字段

@protected 

int ID;

@public 

int age;

@private float price;

}

// 凡是以initXXXX开头的都是构造函数

- (id) init;

//函数名为init 不带参数

- (id) initWithID:(int)new ID;

//函数名为initWithID:带一个init的参数

- (id) initWithID:(int)newID andAge:(int) newAge;

//函数名为initWithID:andAge: 带2个参数,都为int

- (id) initWithID: (int)newID andAge:(int) newAge andPrice:(float) newPrice;

//函数名为initWithID:andAge:andPrice

//带有3个参数,都是为int

 

- (void) setID:(int)newId;

- (int) getID;

// set/get ID

 

- (void) setAge: (int)newAge;

- (int) getAge;

 

- (void) setPrice:(float) newPrice;

- (float) getPrice;

 

- (void) setID:(int)newID andAge:(int)newAge;

// setIDandAge: 2个参数

 

- (void) setID:(int)newID andAge:(int)newAge andPrice:(float)newPrice;

// setID:andAge:andPrice: 3个参数

 

@end

 

 

 

文件Dog.m

#import "Dog.h"

@implementation Dog 

 

- (id) init{

self = [super init];

// super表示父类

// self 表示对象自己

if (self) {

ID = 1;

age = 2;

price = 60.0f;

}

return self;

}

 

- (id) initWithID:(int)newID {

self = [super init]; //初始化父类的构造方法

if(self) {

ID = newID;

age = 2;

price = 60.0f;

}

return self;

}

 

- (id) initWithID:(int)newID andAge:(int) newAge{

return [self initWithID:newId andAge:newAge andPrice:60.0f];

}

- (id) initWithID: (int)newID andAge:(int) newAge andPrice:(float) newPrice{

self = [super init];

if(self) {

ID = newID;

age = newAge;

price = newPrice;

}

return self;

}

 

- (void) setID: (int) newID {

id = newID;

}

- (int) getID {

return ID;

}

 

-(void)setAge:(int)newAge {

age = newAge;

}

-(int)getAge{

return age;

}

-(void) setPrice:(float) newPrice {

price = newPrice;

}

-(float)getPrice {

return price;

}

 

-(void)setID:(int)newID andAge:(int)newAge {

ID = newID;

age = newAge;

price = newPrice;

}

 

@end

 

文件main.m

 

@autoreleasepool

Dog *dog1 = [Dog alloc];

[dog1 init]; 

 

init ID = [dog1 getID];

int age = [dog1 getAge ]; 

float price = [dog2 getPrice];

 

printf("dog1 id is%d age is %d price is %$", ID,age,price );

 

// dog1 id is 1 age is 2 price is 60.000000

// Dog *dog2 = [Dog alloc];

// [dog2 initWithID:100 andAge:26 andPrice:68.88];

 

Dog *dog2 = [[Dog alloc] initWithID:100 andAge:36 andPrice:68.88];

 

ID = [dog2 getID];

age = [dog2 getAge];

price = [dog2 getPrice];

 

printf("dog2 id is%d age is %d price is %f\n", ID,age,price );

//dog2 id is 100 age is 36 price is 68.879997

 

[dog2 setID:2012 andAge:38 andPrice:87.2];

printf("dog2 new id is%d age is %d price is %f\n", ID,age,price );

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics