從第一個應用程式HelloWorld開始:
objective-c版本:
#import <Foundation/Foundation.h>
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
// insert code here...
NSLog(@"Hello, World!");
[pool drain];
return 0;
}
#import <Foundation/Foundation.h> 表示將Foundation.h這個系統檔案的資訊匯入或包含到程式中。
int main(int argc, const char *argv[])
這個函數是一個特殊的函數,用於準確地表示程式將在何處開始執行。該函數返回int類型。
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
這條語句為自動釋放池在記憶體中保留了空間。
NSLog是objective-c庫中的一個函數。
[pool drain] 釋放已經分配的記憶體池。
c#版本的Hello World程式:
using System;
namespace FirstApp
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World");
}
}
}
再來看一個完整的控制台應用程式:
Objective-c版本:
#import <Foundation/Foundation.h>
@interface Fraction : NSObject
{
int numerator;
}
-(void) print;
-(void) setNumerator: (int) n;
@end
@implementation Fraction
-(void) print
{
NSLog(@"%i", numerator);
}
-(void) setNumerator:(int) n
{
numerator = n;
}
@end
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
Fraction * fraction = [Fraction alloc];
fraction = [fraction init];
[fraction setNumerator:10];
NSLog(@"The value of numerator is:");
[fraction print];
[fraction release];
[pool drain];
return 0;
}
我們將此代碼翻譯成C#:
using System;
namespace FirstApp
{
public class Fraction : Object
{
private int numerator;
public void setNumberator(int n)
{
numerator = n;
}
public void print()
{
System.Console.WriteLine(numerator);
}
}
class Program
{
static int Main(string[] args)
{
Fraction fraction = new Fraction();
fraction.setNumberator(10);
Console.WriteLine(@"The value of numerator is:");
fraction.print();
return 0;
}
}
}
在objective-c中,所有類的基類為NSObject, 而c#中為Object.
在objective-c中,
@interface Fraction
@end
@interface用於描述類,類的資料成員及類的方法。
在c#中,表示為:
class Fraction{
}
在objective-c中,
@implementation用於實現@interface中定義的類的方法。
在objective-c中,
int numerator; 這裡的numerator是執行個體變數,定義在類名後面的{}中。
-(void) print; 這裡定義了類的方法print, - 表示類的執行個體方法。
如果定義+(void) print;則表示類的方法。
在c#中,
static void print(){
}
表示類的方法。
在objective-c中,
-(void) setNumerator:(int) n n為setNumerator方法的參數,為int類型。
在c#中為,
void setNumerator(int n){
}
在objective-c中,
-(void) setTo: (int) n over: (int) d;
n, d為參數。這是多參數函數。
還有一種表示形式:
-(void) set: (int) n : (int) d {
Numerator = n; Numerator1 = d;
}
在c#中,
void setTo(int n, int d){
}
在objective-c中,
@property int Numerator1;
@synthesize Numerator1;
為屬性的一中簡寫形式。
在c#中,相當於:
public int Numerator1{ get; set; }
一個完整的objective-c的物件導向的簡單例子:
#import <Foundation/Foundation.h>
@interface Fraction : NSObject
{
int Numerator;
}
-(void) print;
-(void) setNumerator: (int) n;
-(int) Numerator;
-(void) setTo: (int) n over: (int) d;
@property int Numerator1;
@end
@implementation Fraction
-(void) print
{
NSLog(@"%i", Numerator);
}
-(void) setNumerator:(int) n
{
Numerator = n;
}
-(int)Numerator
{
return Numerator;
}
@synthesize Numerator1;
-(void) setTo: (int) n over: (int) d {
Numerator = n; Numerator1 = d;
}
-(void) set: (int) n : (int) d {
Numerator = n; Numerator1 = d;
}
@end
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
Fraction * fraction = [Fraction alloc];
fraction = [fraction init];
[fraction setNumerator:100];
[fraction setNumerator1:99];
NSLog(@"The value of numerator is:");
[fraction print];
NSLog(@"The value of numerator is:%i",[fraction Numerator]);
NSLog(@"The value of numerators is:%i",[fraction Numerator1]);
[fraction setTo:8 over:9];
NSLog(@"The value of numerators is:%i,%i",[fraction Numerator],[fraction Numerator1]);
[fraction set:2 :3];
NSLog(@"The value of numerators is:%i,%i",[fraction Numerator],[fraction Numerator1]);
[fraction release];
[pool drain];
return 0;
}