【正文】
ing, every object should be of type NSObject, and every pointer to an object could be declared as NSObject*. In fact, one can use the type id instead. This is a short and handy way to declare a pointer to any object, and provides dynamic typechecking instead of static typechecking. It is very useful for some weak typing on generic methods. Please note that a null pointer to an object should be set to nil, not NULL. These values are not interchangeable. A normal C pointer can be set to NULL, but nil was introduced in ObjectiveC for pointers to objects. In ObjectiveC, classes are also objects (metaclass instances), and it is possible to declare a pointer to a class. Their null value is Nil. Class declarationIt is hard to show with a single example all the differences between ObjectiveC and C++ for classdeclaration and implementation. Syntax and concepts are interleaved and require explanation. Inthe following, the differences are exposed sequentially and specifically. Attributes and methodsIn ObjectiveC, attributes are called instance data, and member functions are called methods.C++ ObjectiveCclass Foo{double x。public:int f(int x)。float g(int x, int y)。}。int Foo::f(int x) {...}float Foo::g(int x, int y) {...}@interface Foo : NSObject{double x。}(int) f:(int)x。(float) g:(int)x :(int)y。@end@implementation Foo(int) f:(int)x {...}(float) g:(int)x :(int)y {...}@endIn C++, attributes and methods are declared together inside the braces of the class. 陜西理工學(xué)院畢業(yè)設(shè)計 第 24 頁 共 45 頁Methodimplementation syntax is similar to C, with the addition of the scope resolution operator (Foo :: ).In ObjectiveC, attributes and methods cannot be mixed. The attributes are declared in braces,the methods follow. Their implementation lies in an @implementation block.This is a major difference with C++, since some methods can be implemented without beingexposed in the interface. This is detailed later. Briefly, this is a way to clean up header files byremoving unnecessary declarations (“private” methods, and silently redefined virtual methods likedestructors). Please see Section on page 21 for further explanations.Instance methods are prefixed by the minus symbol “”, and class methods by the plus symbol“+”(cf. section on page 18)。 this symbol has nothing to do with the UML notation andthe meaning public or private. The type of the parameters are enclosed in parenthesis, and theparameters are separated by the symbol “:”. Please see Section on the next page for furtherexplanations on the syntax of prototypes.In ObjectiveC, there is no need for a semicolon at the end of a class declaration. Also notethat the keyword to declare a class is @interface and not @class. The keyword @class is onlyused in forward declarations (cf. section on the current page). Finally, if there is no instancedata in a class, the braces, which would enclose nothing, can be ommitted. Forward declarations: @class, @protocolTo avoid cyclic dependencies in header files, the C language supports the forward declaration,that allows the coder to declare a class when the only required knowledge is its existence andnot its structure. In C++, the keyword class is used。 in ObjectiveC, it is @class. The keyword@protocol can also be used to anticipate the declaration of a protocol (cf. section on page 22).C++//In file ifndef __FOO_H__define __FOO_H__class Bar。 //forward declarationclass Foo{Bar* bar。public:void useBar(void)。}。endif//In file include include void Foo::useBar(void){...}ObjectiveC///In file @class Bar。 //forward declaration@interface Foo : NSObject{//In file import import @implementation Foo陜西理工學(xué)院畢業(yè)設(shè)計 第 25 頁 共 45 頁Bar* bar。}(void) useBar。@end(void) useBar{...}@end public, private, protectedOne ma jor feature of the ob ject mo del is data encapsulation, which limits the visibility of data to some parts of the co de, in order to ensure its integrity.C++ ObjectiveCclass Foo{public:int x。int apple()。protected:int y。int pear()。private:int z。int banana()。}。@interface Foo : NSObject{@publicint x。@protected:int y。@private:int z。}(int) apple。(int) pear。(int) banana。@endIn C++, attributes and methods can belong to a public, protected or private scope. Thedefault mode is private.In ObjectiveC, only the instance data can be public, protected or private, and the defaultvisibility is protected. Methods can only be public. However, it is possible to mimic the privatemode, by implementing some methods in the @implementation, without declaring them in the@interface, or using the notion of class category (cf. section on page 25). It does not preventmethods from being called, but they are less exposed. Implementing a method without a previous declaration is a special property of ObjectiveC and has a specific purpose, as explained inSection on page 21.Inheritance cannot be tagged public, protected or private. The only way is public. Inheritance in ObjectiveC looks more like Java than C++ (section 4 on page 21). static attributesIt’s not possible in ObjectiveC to declare a class data attribute (static as in C++). However, itis possible to do the same thing in a different way: use a global variable in the implementation file(optionally with the static C keyword to limit the scope). The class can then use accessors on it (with class methods or normal methods), and its initialization can be done in the initializemethod of the class (cf. section on page 34). MethodsThe syntax for a method in ObjectiveC is different from the syntax for mon C functions. Thissection aims to describe this syntax and adds some information on the underlying message sendingprinciple.陜西理工學(xué)院畢業(yè)設(shè)計 第 26 頁 共 45 頁 Prototype and call, instance methods, class methods? A me