首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > 移动开发 >

object-c基础札记1

2012-11-12 
object-c基础笔记11.方法声明: -(void) setNumerator: (int) d-代表对象方法,代表类方法(静态方法)(void)

object-c基础笔记1

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 <Foundation/NSObject.h>

§   

§  @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:

[this setX:1 andSetY:2]

类实现:

  Fraction.m
§ #import "Fraction.h"
§ #import <stdio.h>
§
§ @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 <stdio.h>
§ #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代表父类对象指针,this代表本对象指针

8.默认的构造函数为

-(void) 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 <Foundation/NSObject.h>
§
§ @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 <stdio.h>
§
§ 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通常这样声明一个函数,可以任务函数名作为第一个参数的标签,所有参数的标签加起来才代表整个函数的意义








-






d

d



热点排行