Chinaunix首页 | 论坛 | 博客
  • 博客访问: 446501
  • 博文数量: 145
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 1060
  • 用 户 组: 普通用户
  • 注册时间: 2013-08-22 11:52
个人简介

专注计算机技术: Linux Android 云计算 虚拟化 网络

文章分类

全部博文(145)

文章存档

2016年(3)

2015年(21)

2014年(75)

2013年(46)

我的朋友

分类: iOS平台

2015-03-03 12:00:05

1.方法声明:

 -(void) setNumerator: (int) d;

-代表对象方法,+代表类方法(静态方法)

(void):返回值

setNumerator:方法名

(int) d:第一个参数类型和参数名

2.obc可调用c++代码(.mm结尾)

3.#import取代#include,可以防止重复include

4.字符串:NSString

NSString *string1 = @"abc";//字符串常量前必须加@

5.类声明:

Fraction.h

§  #import

§   

§  @interface Fraction: NSObject {

§      int numerator;

§      int denominator;

§  }

§   

§  -(void) print;

§  -(void) setNumerator: (int) d;

§  -(void) setDenominator: (int) d;

§  -(int) numerator;

§  -(int) denominator;

§  @end


继承(inheritance)以 Class: Parent 表示,就像上面的 Fraction: NSObject

夹在 @interface Class: Parent { .... } 中的称为 instance variables

没有设定访问权限(protected, public, private)时,预设的访问权限为protected。属性有权限,方法没有权限声明,都是public

方法如果有多个参数,除第一个参数外,后面参数都要有一个lable,这个跟其他语言非常不一样:

-(void) setX:(int)x:andSetY:(int)y;

这个函数名实际上就是setX:andSetY:

调用时第二个及以上的参数也要加上lable:

[self setX:1 andSetY:2]

类实现:

  Fraction.m
§ #import "Fraction.h"
§ #import
§
§ @implementation Fraction
§ -(void) print {
§ printf( "%i/%i", numerator, denominator );
§ }
§
§ -(void) setNumerator: (int) n {
§ numerator = n;
§ }
§
§ -(void) setDenominator: (int) d {
§ denominator = d;
§ }
§
§ -(int) denominator {
§ return denominator;
§ }
§
§ -(int) numerator {
§ return numerator;
§ }
@end


调用:

main.m
§ #import
§ #import "Fraction.h"
§
§ int main( int argc, const char *argv[] ) {
§ // create a new instance
§ Fraction *frac = [[Fraction alloc] init];
§
§ // set the values
§ [frac setNumerator: 1];
§ [frac setDenominator: 3];
§
§ // print it
§ printf( "The fraction is: " );
§ [frac print];
§ printf( "/n" );
§
§ // free memory
§ [frac release];
§
§ return 0;
}

除了基本的数据类型(如int,NSUinteger(用宏定义的无符号int))和结构体(如NSRange)外,其余对象都是用指针来操作的

6.obc只能单继承

7.super代表父类对象指针,self代表本对象指针

8.默认的构造函数为

-(id) init

也可以叫其他名字:

-(Fraction *) initWithNumerator:(int)n denominator:(int)d

{

    self = [super init];

       if(self){//等同于(self != Nil)
 [self setNumerator: n andDenominator: d];//setNumerator是另一个方法,带2个参数,设置两个成员变量的值
}

   return self;  

}

9.属性权限声明

Access.h
§ #import
§
§ @interface Access: NSObject {
§ @public
§ int publicVar;
§ @private
§ int privateVar;
§ int privateVar2;
§ @protected
§ int protectedVar;
§ }
@end


Access.m
§ #import "Access.h"
§
§ @implementation Access
@end


main.m
§ #import "Access.h"
§ #import
§
§ int main( int argc, const char *argv[] ) {
§ Access *a = [[Access alloc] init];
§
§ // works
§ a->publicVar = 5;
§ printf( "public var: %i/n", a->publicVar );
§
§ // doesn't compile
§ //a->privateVar = 10;
§ //printf( "private var: %i/n", a->privateVar );
§
§ [a release];
§ return 0;
}


10.静态方法直至用类名调用:

[ClassName staitcMothedName]


11.init,initialize,load:

initialize相当于java中的static区块:

pubic Class A{

static{

  a = 0;

}

private int a;

}

在类第一次加载的时候调用,并且会先调用父类的static区块

load是程序打开后就会执行的,相当于c++的静态变量,c++最先执行的是静态变量的初始化

load也是先调用父类再调用自己

init:默认的构造函数,先父类再自己

12.异常:

抛出异常:

   NSException *e = [NSException
§ exceptionWithName: @"CupUnderflowException"
§ reason: @"The level is below 0"
§ userInfo: nil];
§ @throw e;

  @try {
 ...
} @catch ( NSException *e ) {
...
 }

@finally

{

...

}

13.id类型用于指代任意对象的指针,相当于void *,但是只能指向对象,不能指向基本类型,obc跟java和c++不一样,不需要知道对象的类型就可以调用方法,即可以用id做为指针调用其指向对象的方法,而不需要强转


14.子类的指针不能赋值给父类的指针


15.-(void) setX:(int)x: andSetY:(int)y;

obc通常这样声明一个函数,可以任务函数名作为第一个参数的标签,所有参数的标签加起来才代表整个函数的意义

16.多态:

id shape;

shape = rect;

[shape print];

shape = square;

[shape print]

分别调用长方形和正方形的print

17.obc可以动态加入方法和属性

@interface string(stringex)

   -(const char*) fun;

@end

即在string中加入了一个fun

18. @property @synthesize:

让编译器自动生成与成员变量名相同的方法(用于get)或setXXX方法(用于set)或两者

在头文件中:
  @property int count;
  等效于在头文件中声明2个方法:
  - (int)count;
  -(void)setCount:(int)newCount;
实现文件(.m)中
  @synthesize count;
  等效于在实现文件(.m)中实现2个方法。
  - (int)count
  {
  return count;
  }
  -(void)setCount:(int)newCount
  {
  count = newCount;
  }
  以上等效的函数部分由编译器自动帮开发者填充完成,简化了编码输入工作量。

声明property的语法为:
@property (参数1,参数2) 类型 名字;

@property(nonatomic,readonly)  int a;

readonly只生产get方法(只读)

nonatomic:不允许多线程访问

使用@property后可以用.语法来访问变量:

比如BaseClass中的字符串变量name被@property声明(非readonly),@synthesize实现,则可以这样访问

BaseClass *base = [[BaseClass alloc] init];

base.name = @"abc";

当然,自己写上get和set方法也可以这样访问

阅读(769) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~