OC Basics for iOS Development-classes and objects

Source: Internet
Author: User

This series of articles is mainly from the individual in the learning forward education-Ouyangjian teacher's iOS development tutorial of the OC language teaching video notes, while watching video, while recording course knowledge points. We recommend that you go through the video, in the process of watching the video recording knowledge Point key words, grasp the focus, and then the scattered knowledge points sorted out. This way of learning will bring more gains, at the same time to do more practice, according to ideas, dictation, rewrite code. Here, thanks to Ouyangjian, learning your course has put me on the path of iOS development.

Be sure to identify a direction to be engaged in, otherwise you will feel confused. Learning iOS development and working in the iOS development industry, that's my choice. See my Blog series, most of which is about C + +, QT, Linux network Programming, OpenGL, Unity3d and so on. On the face of it, there doesn't seem to be much association with iOS development, but the learning experience with these aspects has given me a certain foundation. iOS development is primarily in the OC and Swift languages, and OC is the Foundation for iOS development. As we all know, OC (OBJECTIVE-C) is the object-oriented C, on the basis of C language to increase the object-oriented mechanism and memory management of this pair of wings, so that OC in iOS development.

This series of articles is intended for individuals to learn and share with like-minded apes, and there is no commercial use.

OC is an object-oriented language, and since it is object-oriented, it is less complementary to classes and objects. We know the three main mechanisms of object-oriented in C + +: Inheritance, encapsulation, polymorphism, which is also available in OC. Here's a concrete look at the classes and objects in OC.

Definition of Class

A class is made up of properties and methods that represent the characteristics of a class, and methods represent the actions of a class.

The definition format of the class

The class definition in OC differs from the class definition in C + + in that the header file for the class in OC is ***.h, whereas the source file is a file-***.m with a. m suffix.

The interface is in the header file:

@interface Class Name: base class Name {     // attribute name }// not Here ";" Number      // method declaration @end// end of declaration

The first letter of the class name needs to be capitalized, NSObject is the base class for all OC classes, and the interface contains only the declaration of the method, and the method has a class method and an instance method.

Implementation in the source file:

@implementation class name // Method Implementation @end

The access rights of the class

There are three kinds of access rights: @public, @private, @protected. The default permission for a property is a protected type, and the default permission for the method is the public type.

The constructors in OC start with initwith***, mainly do some initialization work, the destructor is dealloc, if your own write class is not defined, the system will automatically provide a default constructor and destructor.

As an example, the following code:

-(ID) init{ Self=[Super Init]; //using Super Parent class object, self represents this class of object    if(self) {NSLog (@ "in the constructor name%@ age%d%S ", _name,_age,_function_); }    returnSelf ;}-(ID) Initwithname: (NSString *) newname{}-(ID) Initwithname: (nsstring*) newName withage: (int) newage{}-(void) dealloc{//destructors are called when the object disappears completely//Object is destroyed when called, Object counter =0//dealloc do not manually invoke [person Dealloc], should call [person release]; //release destroys the object, the release counter is reduced by one, and the dealloc is automatically called when it is reduced to 0. [Super Dealloc];}

Attribute fields can also be accessed and set using "OBJ->ARRT" in OC, but only if the field must be declared as public type. However, this is not a recommended way to use.

#import<Foundation/Foundation.h>@interfaceperson:nsobject{@public    int_age; NSString*_name;}-(void) Setage: (int) age;-(int) Getage;@end@implementation Person-(void) Setage (int) age{_age=Age ;}-(int) getage{return_age;}@endintMain () {@autoreleasepool { person*person =[[Person alloc] init]; person->age = -; person->name =@ "Little Red"; }    return 0;}

Person *person = [[Person alloc] init] is the object that the person points to has allocated memory space and is initialized. You can also use person *person = [Person New],oc also supports the new operator. Because the age and name fields are declared as public types, you can use pointer invocation to set the properties of an object.

Parameter labels

-(Person *) init: (int) NewID: (int) newage; // no parameter label used -(person *) Initwithid: (int) NewID andage: (int) newage; // Using parameter Labels

The first parameter can be labeled without a label, starting with the second argument. Now the method name is: initwithid:andage:, the parameter label can improve the readability of the method name, but also can be used for overloading the method.

Method overloading

In OC, the overloads of the method are not distinguished by the type of the parameter, but by the number of arguments, as in C + +. Not a strict function overload in OC.

//Method overloads are not refactored in OC because the methods have the same name and the same number of arguments but different parameter types-(int) Dosth: (int) x;-(int) Dosth: (float) x;-(int) Dosth: (int) x: (int) y;-(int) Dosth: (float) x: (float) y;//use parameter tags to form method overloads-(int) Dosth: (int) x: (int) y;-(int) Dosth: (float) x AndY: (float) y;

OC Basics for iOS Development-classes and objects

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.