Boolean Type
The Boolean bool values in C are true and false, while the bool values in objective-C are yes and no. In fact, the bool of objective-C is actually a definition of the signed character type (singed char) (typedef), which uses an 8-bit storage space, yes is defined as 1, no is defined as 0. objective-C does not use bool as the true boolean type that can only save yes or no values. The compiler recognizes bool as an 8-bit binary number. If an integer value longer than 1 byte (short or Int value) is accidentally assigned to a bool variable, then, only the low byte will be used as the bool value, and the bool value will be 0, that is, the NO value. For example, if the low byte is just 0, for example, 8960, The hexadecimal format is 0x2300.
In C, A bool is returned. Some people are used to writing it like this. Assume that IA and IB are two integers.
Return (IA-Ib );
The preceding statement indicates that if the value is not 0, true is returned. But not in objective-C, because in objective-C, 1 is not equal to yes.Therefore, it is wise not to directly compare the bool value with the yes value with the no value..
Enumeration and struct Definition
It is not much different from C ++. It is defined as follows:
Enumeration:
Typedef Enum
{
Kcircle,
Krectangle,
Koblatespheroid
} Shapetype;
Struct:
Typedef struct
{
Int X, Y, width, height;
Shapetype type;
} Shape;
ID Usage
In Example 03.10-shapes-object, the following code is provided:
//--------------------------------------------------
// Draw the shapes
Void drawshapes (ID shapes [], int count)
{
Inti;
For (I = 0; I <count; I ++ ){
Idshape = shapes [I];
[Shapedraw];
}
} // Drawshapes
The input parameter has an ID array object of the type. What does ID mean? It represents identifier (identifier), which is a generic type used to represent all types of objects. ID is actually a pointer pointing to a certain structure.
In the for loop, an ID object is obtained from the ID array sequentially and assigned to the shape. The following sentence
[Shape draw]
What does that mean? In objective-C, square brackets also have a meaning: It notifies an object of what to do. The first is the object, and the second is the operation that needs to be performed by the object. In objective-C, such operations performed with notification objects are calledSend message.
Class declaration, implementation, and instantiation
The following is an interface for the myclass class:
@ Interface myclass: nsobject
{
// Data members of myclass
Int iindex;
Nsstring strname;
}
-(Void) setindex: (INT) index;
-(Void) setname: (nsstring *) Name;
-(Void) print: (INT) index: (nsstring *) Name;
@ End // myclass
@ Interface myclass: nsobject indicates that this is the definition part of myclass and inherits from nsobject.
-(Void) setindex: (INT) index;
Method Definition"-" Indicates that this is the declaration of the objective-C method, (void) indicates the method type, ":" indicates that the function has a parameter, (INT) indicates the parameter type, and index indicates the parameter name, ";" indicates that the method declaration is complete.
If the method has two parameters, such as the print method, the two parameters are separated.
@ End indicates the end of the class interface definition, while the subsequent annotation is a mark that marks the end of the myclass definition.
Implementation of myclass:
@ Implementation myclass
-(INT) setindex: (INT) Index
{
Iindex = index;
} // Setindex
-(Void) setname: (nsstring *) Name
{
Strname = Name;
} // Setname
-(Void) print: (INT) index: (nsstring *) Name
{
Nslog (@ "The % d's name is % @", index, name );
} // Print
@ End // myclass
@ Implementation is a compiler instruction that provides code for myclass until @ end ends. The intermediate method implementation can be implemented in sequence according to the definitions in the interface, or the methods that do not appear in the interface can be written.
Run the following code to instantiate the myclass class and call its method:
Myclass myobj = [myclass new];
[Myobj setname: @ "onename"];
[Myobj print: 5: @ "onename"];
Inheritance
What is inheritance? What are the benefits of inheritance? I will not introduce it here. If I don't understand it, I will look for an object-oriented book to learn it again.
In objective-C, the inherited syntax is as follows:
@ Interface myclass: nsobject
The identifier after the colon is the class to be inherited. In objective-C, only single inheritance can be implemented.
For example, Class A inherits from Class B.
Superclass: Class B can be called the superclass of Class A, also known as the parent class;
Subclass: Class A is a subclass of Class B, also known as the Child class.
Inherited Working Mechanism:
Class A inherits from Class B. Class A has a method named functiona. Class B also has such a method. When Class B's method functiona is called, the Class B method will be executed, instead of Class A, because this method of Class A has been overwritten by Class B.
If Class B does not override the method of Class A, the compiler will first look for it in class B. If it is not overwritten, it will go to the superclass of Class B, that is, to search for Class A until it is found.
If Class B overrides the method functiona in Class A, but class B's functiona still wants to call its superclass method, it needs to be used.SuperThe keyword is as follows:
@ Implementation classb
-(Void) functiona: (INT) ivalue
{
If (ivaule = 5)
Ivalue = 10;
[Super functiona: ivalue];
}
@ End
Compound
The combination in programming is like composing in music: multiple components are combined for use to obtain the complete work. There is nothing to talk about here.
However, a problem may occur after coding "05.01carparts" according to the book, that is, after you click the run button, the program will pause after the engine is output, after the description function of tire, then there will be a prompt "thread 1: stopped at breakpoint 1". I didn't know why when I first met this, and then I continued to explore it, in fact, this is not a bug. The code itself is okay, but I don't know why it stops here during execution. Note that I am talking about pause, so the method is to let it continue execution, choose "product"> "debug"> "continue" from the menu and continue the execution.
Inheritance establishes the "is a" relationship between objects. That is to say, when a is a B, the relationship between them is inherited.
Composite is a relationship of "has a". If a has a B, the relationship between them is composite.