IPhone Study Notes (3)

Source: Internet
Author: User
In this chapter, you will learn some object-oriented programming skills and how to compile classes in objective-C. What is an object?

We use a daily example to explain it. Assume that you own a car. Obviously, this car is an object. Now you don't have any car, so your car may be made in Detroit, Japan, or any factory in the world, which is abstract. You may drive it, cheer it up, wash the car, and so on. These actions are aimed at this object. (These classes and objects have the same meanings as other object-oriented programming languages, so we will not talk much about the definition)

Now let's take a quick look at the actual examples. The following assumes that you do not understand classes and methods. The program you write may look like this:

The following is an object-oriented method.

In objective-C, class declarations are divided into two parts.

  1. Definition: starts with @ interface and ends with @ end. Defines the class members, methods, return types of methods, parameters, and so on.
  2. Implementation part: the class name is the same as the class name when it is declared, as shown in the following example: fraction. For the implementation in the definition.
# Import <Foundation/Foundation. h> @ interface fraction: nsobject {int numerator; int denominator;}-(void) print;-(void) setnumerator :( INT) N;-(void) setdenomainator :( INT) d; @ end @ implementation fraction-(void) print {nslog (@ "% I/% I", numerator, denominator);}-(void) setnumerator :( INT) n {Numerator = n ;}- (void) setdenominator :( INT) d {Denominator = D ;}@ endint main (INT argc, char * argv []) {NSAID utoreleasepool * Pool = [[NSAID utoreleasepool alloc] init] fraction * myfraction; // create an instance myfraction = [fraction alloc]; myfraction = [myfraction init]; // assign the value to the variable defined above [myfraction setnumerator: 1]; [myfraction setdenominator: 3]; // display the output result nslog (@ "the value of myfraction is :"); [myfraction print]; [myfraction release]; [pool drain]; return 0 ;}

In order to get familiar with the call method of this definition, I made one by myself. During debugging, I always reported an error: cannot init a class object was found for half a day. It turns out that the instance was created as follows.

// Create an instance myfraction = [fraction alloc]; myfraction = [myfraction init];

I mistakenly wrote myfraction = [myfraction init]; myfraction = [fraction init]; myfraction is the instance, and fraction is the class name. No wonder that error is reported. Haha, but this gives a deeper impression on object initialization in objective-C, and it is not a good thing to make a mistake.

Now, we will analyze the class definition and related usage.

First, define a class and give it a name (which is the same as all object-oriented programming languages). Then, in objective-C, specify the parent class of the class, that is, the inherited class. As follows:

@interface Fraction:NSObject

Nsobject is equivalent to the object in C # And is the root class.
Then define the class member and variable in the middle of the braces. (The return value is the same as the variable name in other languages)

 int numerator; int denominator;

Next is the definition of the method. Three methods are defined as follows: print, setnumerator, and setdenomainator. The-sign before (void) indicates that the method is an instance method, and the corresponding + indicates a class method. This is a lot different from the programming languages I used before. (Void) needless to say, you should be quite familiar with the return type of the representation method. The method names, such as print and setnumerator, are displayed.

-(void)  print;-(void)  setNumerator:(int) n;-(void)  setDenomainator:(int) d;

Note that the parameters in objective-C must have a colon before them, and multiple parameters must have multiple colons. For example:

-(void) exampleMetho:(int) weight zhongliang:(int) height 

Zhongliang after weight is used to mark the role of the parameter.
Then, the implementation part @ implementation name must be the same as the definition part name @ implementation fraction. The next step is to implement the specific code implementation of the previously defined method.

Finally, the specific calling process in the main function.

NSAID utoreleasepool * Pool = [[NSAID utoreleasepool alloc] init] after the project is created, the system will automatically add this segment. The detailed principles will be explained in later memory management.

Create an instance: fraction * myfraction = [fraction allock] is equivalent:

Fraction *myFraction;myFraction=[Fraction alloc];myFraction=[myFraction init];

Release memory: [myfraction release]; this is different from other automatic garbage collection methods.

 

 

 

 

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.