The way to Practice iOS: Objective-c classes, objects, and methods 1

Source: Internet
Author: User

1. Concepts of classes, objects, and methods

A class is a unique representation of an object, each containing its own private information, by altering and providing access to that data.

Therefore, OBJECTIVE-C uses a specific syntax to apply methods to classes and instances:

[Classorinstance Method];

or understood as the delivery and acceptance of messages

[Receiver message];

Examples are as follows:

1 New ] init]; 2 [MyCar dive];

Detailed description about a processing fraction objective-c class

The code is as follows:

1 //2 //main.m3 //AddressBook4 //5 //Created by admin on 15/11/26.6 //copyright©2015 Year Admin. All rights reserved.7 //8 9 ///////////////////fraction.h///////////////////Ten  One #import<Foundation/Foundation.h> A  - //--------@interface Part-------- -  the @interfaceFraction:nsobject -@propertyintsum; --(void) print; --(void) Setnumerator: (int) n; +-(void) Setdenominator: (int) D; -  + @end A  at  - //---------@implementation Part----- -  - @implementationFraction - { -     intnumerator; in     intdenominator; - } to //If there is no composition statement, the attribute sum automatically generates an instance variable called _sum; + //@synthesize sum; -  the-(void) Setdenominator: (int) d * { $Denominator =D;Panax Notoginseng      - } the  +-(void) Setnumerator: (int) n A { theNumerator =N; + } -  $-(void) Print $ { -NSLog (@"%i/%i", numerator, denominator); -NSLog (@"_sum is%i", _sum); the } - Wuyi @end the  -  Wu //-------------- Program part--------------- - intMain () About { $ @autoreleasepool { -Fraction *myfraction =[[Fraction alloc] init]; -[Myfraction Setnumerator:1]; -[Myfraction Setdenominator:3]; A[Myfraction setsum:3]; +          theNSLog (@"This fraction is:"); - [myfraction print]; $          theNSLog (@"sum is%i", myfraction.sum); the     } the     return 0; the}

Wherein, the @interface part is used to describe the class and the method of the class, the @implementation part is used to describe the data (the class object's instance variable stores the data), and implements the actual code that declares the method in the interface;

@interface

When defining a new class, first tell the OBJECTIVE-C compiler the reason for the class. In other words, it must be named for its parent class. Next define the various operations methods, and the part also needs to list some elements, called attributes.

@interface Newclassname:parentclassname

Propertyandmethoddeclarations;

@end

Here I have a question about the difference between a property and an instance variable:

Member variables:

1. is declared in the {}

2. First there is a member variable after the attribute is said.

3. Through the permission modifier @protected, @private, @public more of its access rights.

Property:

1. First @property declaration and @synthesize synthesis

2. Attribute is a new mechanism of OC language

3. The object belonging to the class, by generating an object of that class, we can gain access to it.

It is not difficult to find that the member variables can be freely defined in @interface and @implementation, and the attributes are defined at interface.

The first instance variables in objective-c need to be declared with @public that are used by the external class and used internally by @private or @protect. Objective-c I added attributes, I think Apple's consideration should be that attributes are used externally and the strength variables are primarily used within the program. This facilitates the separation of the code, because the compiler provides the corresponding instance variable for the property directly (you can also specify the instance variable for that property manually) and the Getter/setter method.

Reference website:

http://m.blog.csdn.net/blog/codebat/39347125

http://blog.csdn.net/addychen/article/details/39525681

@implementation part

This section contains the implementation of the Code for the interface section and requires the development of the data types stored in the class object.

The format is as follows

1 @implementation Newclassname 2 { 3       memberdeclarations;4}5methoddefinitions;6 @end

Program section

Fraction *myfraction = [[Fraction alloc] init]; is a compound statement,

Fraction *myfraction;

myfraction = [Fraction alloc];

Myfraction = [myfraction init];

When I began to study, I always used myfraction as a pointer, because the definition of the form is the pointer type, the book is simply a reference, in fact, is a pointer.

Next section, in detail to distinguish between C + + and OC "." and "->?" Operator. Thus synthesize the comprehension of C-type language.

The way to Practice iOS: Objective-c classes, objects, and methods 1

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.