Objective-c object-oriented basics, objective-c object-oriented
Currently, mobile development is booming. Today, I am studying the recently started Objective-c basic tutorial at home. net is very different. In order to better learn and understand Objective-c, we will record the learning knowledge points in the form of notes for easy reference.
In Objective-c, the class is divided into two parts: declaration and implementation. The Declaration uses the keyword @ interface to implement the use of the keyword @ implementation. The @ symbol can be understood as an extension of the c language, in. net uses the Class keyword, as shown below:
#import <Foundation/Foundation.h>@interface Person : NSObject{ @private NSString* firstName; NSString* lastName;}
//getter- (NSString*) firstName;- (NSString*) lastName;
//setter- (void) setFirstName : (NSString*) fName;- (void) setLastName : (NSString*) lName;- (NSString*) description;+ (NSString*) breath;@end@implementation Person-(NSString*) firstName{ return firstName;}-(NSString*) lastName{ return lastName;}-(void) setFirstName:(NSString *)fName{ self->firstName = fName;}-(void) setLastName:(NSString *)lName{ self->lastName = lName;}-(NSString*) description{ return [[self->firstName stringByAppendingString:@" " ] stringByAppendingString:self->lastName];}+(NSString*) breath{ return @"air";}@end
Observe the code and draw the following conclusions:
0. # import the header file. This command can ensure that the header file is only contained once. No matter how many times this command appears in the file, the # include command is used in C.
1. Each method is preceded by a "+" or "-" symbol. + indicates that this method is called by class methods, and-indicates that this method is called by instances.
2. The self keyword table references the instance object itself, which is similar to the this keyword in. NET.
3. NSObject is a base class in Objective-c. We recommend that you inherit NSObject from custom classes. Objective-C does not support multi-inheritance. However, you can implement multi-inheritance through category and protocol.
4. The description method inherits from NSObject and is overwritten in Person. If the instance object is directly used in Objective-c, the description method is called by default, which is equivalent to the ToString in. NET.
5. In Objective-c, the method call is between a pair of square brackets in the form of [instance method: parameter ......].
6. super is used to call the implementation method in the superclass and is used as the call target.
NSLog output format characters:
| % D |
Output integer |
| % @ |
Output object. The description method of the object is called by default. |
| \ N |
Line feed |
The test code for the Person class is as follows:
Int main (int argc, const char * argv []) {Person * person = [Person new]; [person setFirstName: @ "xiao"]; [person setLastName: @ "ming"]; NSLog (@ "first name: % @", [person firstName]); NSLog (@ "last name: % @", [person lastName]); NSLog (@ "full name: % @", person); // call the description method return 0 by default ;}
The program output is as follows:
The above is the initial understanding of Objective-C. If there are any errors, please point out.