Objective-c Object-oriented

Source: Internet
Author: User

Reprint, quote Please specify the source!

Class of Objective-c

I. Introduction to grammar 1, Class 1),. hFiles: Classes ofStatementFile that declares instance variables and methods. The declaration of the class uses the keyword@interfaceAnd@end。 The method in the. h simply declares that it is not implemented, that is, only the method name, return type, received parameters and types, and does not write the method's internal code "! 2),. MFiles: Classes ofImplementfile that implements the method declared in. h. The implementation file for the class uses the keyword@implementationAnd@end。 2, the instance variable instance variable declaration and the C language definition variable method is the same, uses "variable type variable name" The syntax of the format. 【IDType variable representationany object type"3, the instance variableScope: 1), @public (public) on the premise of having objects, any place can be directly accessed. 2), @protected (protected, default is @protected) can only be accessed in the object methods of the current class and subclass. 3), @private (private) can only be accessed directly in the object and object methods of the current class. 4, Method 1), the Declaration and implementation of the method, must be+Yes-Number start + indicateclassMethod-RepresentsObjectMethod 2), all method scopes declared in. h are public. Second, create OC Class 1, right-click the project file, select New file 2, select Cocoa Cocoa class

3. Enter the class name (the first letter is usually uppercase) and select the parent class, language

This creates a class with a class named people, and the parent class is nsobject. 4, after the completion of the project, more than 2 files
* People.h is the declaration file for a class, and PEOPLE.M is the implementation file for the class. * By default, these two filenames match the class name * The compiler compiles only. m files and does not compile. h files.

Third, class code resolution 1, people.h-class declaration file

#import

@interface People:nsobject

@end

1), OC uses @interface to declare a class, @interface followed by the class name people. 2), class name people the colon after: Represents inheritance, People:nsobject indicates that the people class inherits from NSObject. 3), @end represents the end of a class declaration.  4), #import, because NSObject is declared in Foundation.h. 2, the implementation of the people.m-class file

#import "People.h"

@implementation people

@end

1), because people this class declaration in the People.h file, so you want to #import "People.h".

2), using the keyword @implementation in OC to implement a class, @implementation people represents the class to be implemented.

3), @end represents the end of the implementation of the class.

Iv. adding member variables Typically, we define instance variables in the class's header file, which is the declaration file for the class. 1. Add instance variables to people

#import

@interface People:nsobject

{

int _age; Age

float _weight; Weight

}

@end

1), added an int type member variable _age and float type member variable _weight, by default the scope of the member variable is @protected [protected], that is, can be accessed within the people class and subclass.

2), the instance variable must be written in curly braces {} .

V. METHODS of Addition

The two instance variables _age and _weight are defined earlier, they are scoped @protected and cannot be accessed directly by the outside world. To ensure the encapsulation of the data, we can provide the get and set methods of the _age and _weight to allow the outside world to access _age and _weight indirectly. The following is an example of the get and set methods of _age.

1. Declaring the method in People.h

#import

@interface People:nsobject

{

int _age; Age

float _weight; Weight

}

-(void) Setage: (int) age; The set method declaration of age

-(int) age; The set method declaration of age

@end

1),-(void) Setage: (int) age; is the declaration of the set method, which represents the class method , (void) represents the return type, Setage: is the method name, "Note that the Colon is also part of the method name, and a colon corresponds to a parameter ", (int) is the argument type, and age is the parameter name.

2),-(int) age; is the declaration of the Get method, which has no arguments.

2, the realization method in the PEOPLE.M

#import "People.h"

@implementation people

Implementation of Set method

-(void) Setage: (int) age{

_age = age;

}

Implementation of the Get method

-(int) age{

return _age;

}

@end

1), set method assigns the parameter age to the instance variable _age. 2), the Get method returns the instance variable age. VI. create an object before you use an object, you first call the class's constructor to generate the object. OC by sending a message to the class toCreateOneObject, you can create an object by sending a ALLOC message:syntax: [class name alloc]; AllocAfter execution, the object needs to be completed.Memory allocation, but it hasn't been initialized yet. The method of initializing the object is calledInitialize Method。 Syntax: [[Class name Alloc]Init]; Methods in *oc can be used in a nested bar. This makes it possible to generate an object. (You can also use new to generate an object directly) * Create a People Object

#import

#import "People.h"

int main (int argc, const char * argv[]) {

@autoreleasepool {

People *p1 = [[People alloc] init];

}

return 0;

}

1), because you want to use the People class, include #import "People.h".

2), the method call in OC uses [], the left side of the parentheses is the caller (class or object), and the right side is the method.

3), allocating memory. + (ID) alloc; The method is a class method whose return value is the ID type.

4), initialize. The Init method is an object method.

* Destroy objects: In the age of the arc (auto-release) mechanism, you should manually release each object once it is created.

That is: [P1 release];

Objective-c Object-oriented

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.