2009年11月18日星期三

Objective-C 學習筆記

最近正在學習iPhone 開發,記錄一下

常用 Key words

. Messaging
. Import Syntax
. Declaring Method Headers
. Inheritance Synax
. property and synthesize  keywords
. interface and implementation Declarations
. protocols
. self keywords.
. Id keyword

Messaging:  --可以理解為method (c/c++/java)
Example:
[object message]; 
similar to :
object.method();
object->method();

Messaging with parameters

Example: 含參數的例子
[object message: param1  withParameter:param2];
Similar to:
object.method(param1, param2);
object->method(param1,param2);


Nestd Messages :

example:嵌套調用
[object secondMessage:[object message]]
Similar to :
object.secondMessage(object.message());
object.secondMessage(object->message());

The import statement
example:
#import <Library.h>
#import "class.h"
Similar to include and import statement in  C/C++/Java and no include guards required with Objective-C imports

Method Headers
Example:
-(returnType)methodName:(dataType) param1  //對象方法聲明
Called by:
[object methodName:param1] //對象方法調用
a + rather than - before the method signifies that the method is static  //"+"表示靜態方法(class的方法) "-"表示 對象的方法
Similar to :
returnType methodName(dataType param1)

Declaring Inheritance //繼承關係
example:
ClassName:ParentClass<Protocol>
Similar to:
ClassName:ParentClass<Interface>
ClassName  extends ParentClass implements interface
Note:
There is no mulitiple inheritance in Objective-C //Objective-C 中不存在多繼承


@property declaration
Used to create getters and setters for to specified variable. Place in the implementation of a class   //感覺是getter和setter的一個省事的做法
Example:
@property dataType variablename //在*.h文件中declare 在*.m文件中使用@synthesize與之對應


typical usage of variables defined using @property:
className.variable = value
variable = className.variable

@interface
Declare class member variables, and methods in the interface // @interface 作用是聲明類的變量和方法


@interface ClassName: ParentClass <Protocol>
{
    dataType variableName;
}
@property data;
-(returnType)methodName:(dataType)param1
@end


@synthesize
Used to synthesize the getter and setter declared with the @property directive //與@property對應
example:
@synthesize variableName


@implementation
This is where you implement the actual class.  //具體實現一個類,或者是類的實現


@implementation ClassName
@synthesize data;
-(returnType)methodName:(dataType)param1
{
    ....Method Details...
}
@end


Protocols
implemented using <> brackets when creating class interface //在創建interface的時候使用的
example:
@interface ClassName:ParentClass <Protocol>
similar to interface in Java, and C++ //類似Java C++的interface/abstract class 的實現


Self Keyword
Example:
[self message]
similar to :
the this keyword used in Java and C++.
Self must be used when calling a non-static method within the same object // self只能調用非靜態的方法


id keyword
The id keyword is used as generic identifier to identify any class
Similar to the Object keyword in java and  to void pointer(void*) in C++ //我的理解是最泛的一個標識

記錄一下,備忘!

没有评论:

发表评论