Objective-C: Object-oriented Programming

Source: Internet
Author: User

Objective-CStudy NotesObject-orientedProgramming is the content to be introduced in this article.Object-orientedFor senior C ++ programmers,Object-orientedProgramming is not a new concept. The concepts of classes, objects, instances, and methods are no longer redundant. This article focuses onObjective-CWhat are the differences between OOP and C ++ that we are familiar.

 
 
  1. Point : NSObject  
  2. {  
  3.   int X;  
  4.   int Y;  
  5. }  
  6. - (void) SetX: (int) X;  
  7. - (void) SetY: (int) Y;  
  8. - (void) Show;  
  9. // Point 

The following is a sentence-by-sentence analysis.

"Point" tells the compiler: "This is the interface defined for the new class named Point .", The subsequent "NSObject" tells the compiler that the Point class is based on the NSObject class. This statement indicates that each Point class is an NSObject, and each PointInheritanceAll actions defined by the NSObject class.

The content in braces is a template used to create a large number of new Point objects. It indicates that when a new Point object is created, the object consists of two elements, X and Y. Each created Point object will have its own X and Y.

The subsequent code looks like a C function prototype. In Objective-C, they are called method declarations. The method declaration specifies the name of each method, the type of the return value of the method, and some parameters.

The short-line "-" above indicates that this is the declaration of the Objective-C method, which is a way to distinguish between the function prototype and the method declaration.

The short line is the return type of the method, which is located in parentheses. void indicates no return value. The Objective-C method can return the same type as the C function: Standard integer, floating point, and struct type), pointer, object reference, and structure.

The method type is followed by the method name. If the method uses parameters, you must use a colon at the end of the name. This colon is part of the name and tells the compiler that a parameter will appear later.

The parameter type is specified in parentheses, followed by the parameter name.

The last "Point

 
 
  1. - (void) SetX: (int) thisX  
  2. {  
  3.   X = thisX;  
  4. } // SetX  
  5.  
  6. - (void) SetY: (int) thisY  
  7. {  
  8.   X = thisY;  
  9. } // SetY  
  10.  
  11. - (void) Show  
  12. {  
  13.   NSLog(Point at (%d, %d)", X, Y);  
  14. } // Show  
  15. // Point 

The following is a sentence-by-sentence analysis.

@ Implementation is a compiler instruction that provides code for a class. Class Name Point) after @ implementation, there is no semicolon at the end of the row.

The next step is the definition of each method, which does not need to appear in the order of @ interface commands. You can even define methods in @ implementation that do not have corresponding declarations in @ interface. You can regard them as private methods and use them only in class implementation.

The first line in the method definition looks very similar to the @ interface declaration. The main difference between the two is that there is no semicolon at the end. In addition, we need to note that we have renamed the parameters of the method, @ interface uses the names X and Y in the method declaration to tell readers the usefulness of parameters. In the specific implementation, we must distinguish the parameter name from the instance variable name, so we need to rename the parameter.

The SetX method has almost the same structure as the SetY method. For the Show method, note that there is no colon at the end of the method name, indicating that it does not use any parameters.

The final "@ end" code tells the compiler that we have completed the implementation of the Point class.

Instantiate object

Instantiate an object is to create a new object based on the statement we wrote earlier. To create a new object, we first familiarize ourselves with a new operator.

In C, programmers use square brackets to reference array elements. In Objective-C, square brackets also have other meanings: they are used to send messages to an object and notify them to perform some operation. In square brackets, the first item is the object, and the rest is the message you need to send. Example:

 
 
  1. [Point new] 

Objective-C has an excellent feature. You can use classes as objects to send messages to classes. In this example, we send a new message to the Point class, and the notification Point class creates a new object. The following is the complete code for instantiating objects.

 
 
  1. int main (int argc, const char* argv[]){  id thisPoint;  
  2.    thisPoint = [Point new];   
  3.     [thisPoint SetX: 0];   
  4.      [thisPoint SetY: 0];     
  5.      [thisPoint Show];  
  6.  }    
  7. // main 

The following is a sentence-by-sentence analysis.

This main () function is no different from the main () function in a common C program. The argc parameter stores the number of startup parameters, because the program name is often used for parameter transfer, therefore, argc is usually 1 or larger. The argv array is used to save the startup parameter. argv [0] is usually the program name. If there are other startup parameters, they are stored in argv [1], argv [2], and so on.

Id indicates identifier, which is a generic type used to represent objects of any type. In fact, id is the pointer to an object.

By sending a new message to the Point class, an instance of the Point class is created. thisPoint stores pointers to this city.

The next step is to set the elements X and Y by sending the SetX and SetY messages to the thisPoint object. It should be noted that, similar to the method declaration, when a message sent to an object by the [] operator needs to carry parameters, a colon is used after the message, and the parameters are followed by the colon.

Finally, the Show message is sent to the thisPoint object to display its related information. You must also note that when the sent message does not have a parameter, you must not use a colon.

Inheritance

When writing object-oriented programs, there is a certain relationship between the classes and objects you create. They work together to implement the corresponding functions of the program. When dealing with the relationship between classes and objects, OOP has an important feature called inheritance. inheritance can be used to define a new class with all the functions of the original class.

When the @ interface part of the previous article declares a new class, it actually uses the inherited syntax.

 
 
  1. @interface Point : NSObject  

As shown in the preceding example, the identifier after the colon is the class to be inherited. In Objective-C, objects can be inherited from non-classes, but if Cocoa is used, the object will be inherited from NSObject, because NSObject provides a lot of useful features. The simplest inheritance code of @ interface can be as follows:

 
 
  1. @interface MyPoint : Point@end  // MyPoint 

In this case, the MyPoint class inherits all the data members and methods of the Point class. In the following text, we use superclass to describe the inherited classes and subclass to describe the inherited classes. Here, Point is the superclass of MyPoint, and MyPoint is the subclass of Point.

When writing a new subclass, we sometimes need to add a new method to introduce a unique feature in the class. Sometimes, you may need to replace or enhance the existing methods defined by a superclass of the new class.

Objective-C provides some method to redo the method, and still calls the implementation method of the superclass. To call the implementation of an inherited method, super must be used as the method call target.

Continue to follow the example given above, assuming we found that the previously used coordinate deviation, the actual X coordinate should be the original X-10. We can rewrite the Show () method of the Point class in the subclass MyPoint as follows.

 
 
  1. @implementation MyPoint   
  2. - (void) Show{  XX = X - 10;  [super Show];  
  3. }    
  4. // Show @end  // MyPoint 

From the above Code, we use the corresponding code of the superclass by sending the Show message to super.

Here, super is neither a parameter nor an instance variable. When sending a message to super, it actually sends a message to the super class in the request of Objective-C. If the message is not defined in the superclass, Objective-C willInheritanceLink to continue searching for the corresponding message.

Summary: DetailsObjective-CStudy NotesObject-orientedI hope this article will help you with programming.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.