从第一个应用程序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;
}