Objective-C代码学习大纲(2)

2011-05-11 14:06 佚名 otierney 字号:T | T
一键收藏,随时查看,分享好友!

本文为台湾出版的《Objective-C学习大纲》的翻译文档,系统介绍了Objective-C代码,很多名词为台湾同胞特指词汇,在学习时仔细研读才能体会。

AD:干货来了,不要等!WOT2015 北京站演讲PPT开放下载!



创建classes

@interface

 

  1.   
  2.   
  3. Fraction.h  
  4.   
  5. #import  
  6.   
  7. @interface Fraction: NSObject {  
  8.   
  9. int numerator;  
  10.   
  11. int denominator;  
  12.   
  13. }  
  14.   
  15. -(void) print;  
  16.   
  17. -(void) setNumerator: (int) n;  
  18.   
  19. -(void) setDenominator: (int) d;  
  20.   
  21. -(int) numerator;  
  22.   
  23. -(int) denominator;  
  24.   
  25. @end  

NSObject:NeXTStep Object 的缩写。因为它已经改名为 OpenStep,所以这在今天已经不是那么有意义了。

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

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

没有设定存取权限(protected, public, private)时,预设的存取权限为 protected。设定权限的方式将在稍后说明。

Instance methods 跟在成员变数(即 instance variables)后。格式为:scope (returnType) methodName: (parameter1Type) parameter1Name;

scope 有class 或 instance 两种。instance methods 以 - 开头,class level methods 以 + 开头。

Interface 以一个 @end 作为结束。

@implementation

 

  1. Fraction.m 
  2.  
  3. #import "Fraction.h" 
  4.  
  5. #import 
  6.  
  7. @implementation Fraction 
  8.  
  9. -(void) print { 
  10.  
  11. printf( "%i/%i", numerator, denominator ); 
  12.  
  13.  
  14. -(void) setNumerator: (int) n { 
  15.  
  16. nnumerator = n; 
  17.  
  18.  
  19. -(void) setDenominator: (int) d { 
  20.  
  21. ddenominator = d; 
  22.  
  23.  
  24. -(int) denominator { 
  25.  
  26. return denominator; 
  27.  
  28.  
  29. -(int) numerator { 
  30.  
  31. return numerator; 
  32.  
  33.  
  34. @end 

 

Implementation 以 @implementation ClassName 开始,以 @end 结束。

Implement 定义好的 methods 的方式,跟在 interface 中宣告时很近似。

把它们凑在一起

 

  1. main.m 
  2.  
  3. #import 
  4.  
  5. #import "Fraction.h" 
  6.  
  7. int main( int argc, const char *argv[] ) { 
  8.  
  9. // create a new instance 
  10.  
  11. Fraction *frac = [[Fraction alloc] init]; 
  12.  
  13. // set the values 
  14.  
  15. [frac setNumerator: 1]; 
  16.  
  17. [frac setDenominator: 3]; 
  18.  
  19. // print it 
  20.  
  21. printf( "The fraction is: " ); 
  22.  
  23. [frac print]; 
  24.  
  25. printf( "\n" ); 
  26.  
  27. // free memory 
  28.  
  29. [frac release]; 
  30.  
  31. return 0; 
  32.  

 

output

  1. The fraction is: 1/3 
  2.  
  3. Fraction *frac = [[Fraction alloc] init]; 

这行程式码中有很多重要的东西。

在 Objective-C 中唿叫 methods 的方法是 [object method],就像 C++ 的 object->method()。

Objective-C 没有 value 型别。所以没有像 C++ 的 Fraction frac; frac.print(); 这类的东西。在 Objective-C 中完全使用指标来处理物件。

这行程式码实际上做了两件事: [Fraction alloc] 唿叫了 Fraction class 的 alloc method。这就像 malloc 记忆体,这个动作也做了一样的事情。

[object init] 是一个建构子(constructor)唿叫,负责初始化物件中的所有变数。它唿叫了 [Fraction alloc] 传回的 instance 上的 init method。这个动作非常普遍,所以通常以一行程式完成:Object *var = [[Object alloc] init];

[frac setNumerator: 1] 非常简单。它唿叫了 frac 上的 setNumerator method 并传入 1 为参数。

如同每个 C 的变体,Objective-C 也有一个用以释放记忆体的方式: release。它继承自 NSObject,这个 method 在之后会有详尽的解说。

posted on 2015-04-22 11:54  Hai_阔天空  阅读(157)  评论(0编辑  收藏  举报

导航