Personal summary of the know-how----OC object as a method of continuous, focus is also difficult.

Source: Internet
Author: User

Believe that there are many beginners, OC in the object as a method of continuous transmission in one or two objects may be OK, more may be around the halo ... I will then share my personal knowledge of this piece of the summary or tips or tricks.

I think with "mantis stalks Oriole" comparison image, the key point on a ring, how to play an analogy or to see personal understanding ...

For example: A customer wants to go to a restaurant, a restaurant has a chef, and a chef has ingredients. The above objects are: Customers, restaurants, cooks, ingredients;

First, to understand the 2 paragraphs about the customer code.

#import <Foundation/Foundation.h>"Restaurant.h"@interface Custom: NSObject// function Meal -(void) Eatfood: (Cook *) CKP; @end

This is the Custom.h file, CKP refers to the chef, because only the cook cooked the meal, the customer can eat. The header file is imported into the Restaurant.h because the chef belongs to the restaurant.

#import"Custom.h"@implementation Custom//Implementation features-(void) Eatfood: (Cook *) ckp{//Judging if there is no rice    if(CKP! =Nil) {NSLog (@"Open Eat"); if(Ckp->_age < -) {NSLog (@"but not enough to eat!"); }Else{NSLog (@"I'm finally fed up!"); }            }Else{NSLog (@"Eat yarn"); }} @end

This is the CUSTOM.M file, (age to show the chef's age, 20 years old chef do bad food, so did not eat full, the situation in this article is not related to this topic, see not see please friends casually).

Two. Look at these 2 paragraphs about the restaurant code.

#import <Foundation/Foundation.h>"Cook.h"@interface Restaurant: NSObject// sell Rice function + (Cook *) Sellfood: (int) money; @end

Cooking requires the chef, so import the Cook.h header file in the Restaurant.h header file!

Restaurants do not need to deal with the attributes of food, so we create a class method, so that the program executes more efficiently. (after the principle is described, there is not much to be described here).

#import"Restaurant.h"@implementation Restaurant//realize the function of selling rice+ (Cook *) Sellfood: (int) money{//Judging the money is not enough    if(Money >=Ten) {Cook* CKP = [CookNew]; Food* fp = [foodNew]; FP->_weight =2; CKP->_age = -; returnCKP; }Else    {        returnNil; }} @end

In the restaurant.m file to carry out the food selling behavior, must have the money to eat Ah! (I have not eaten the Overlord meal, the interested friend may try the Overlord meal feeling!)

Three. Take a look at these 2 pieces of code about the chef. (a tailor who doesn't want to cook is not a good soldier-----Napoleon).

#import <Foundation/Foundation.h>"Food.h"@interface Cook:nsobject  // attribute {@public    int  _age;} // function Cooking -(void) Makefood: (Food *) F; @end

Have you found a little "routine", the cook to cook for the food bar! Import the Food.h header file into the Cook.h header file.

#import"Cook.h"@implementation Cook//Implement cooking function-(void) Makefood: (Food *) f{if(f!=Nil) {        if(_age > -) {NSLog (@"Cooking"); }Else{NSLog (@"It 's not good to eat ."); }            }Else{NSLog (@"It 's not good to eat ."); }} @end

Here's the cook for the cook!

Four. Last look at the 2 short code for the ingredients. (Leek fried kidney, you like to eat!).

#import <Foundation/Foundation.h>@interface food:nsobject// property {    @public     Double _weight;} // function  sweet and full belly -(void) fulltripe; @end

The ingredient is the last object, so he doesn't need to import any other header files. (The function is not only to fill the belly Oh!).

" Food.h " @implementation Food // function Implementation -(void) fulltripe{    if2) {         NSLog (@ "  eat full ");    } Else {        NSLog (@ " not full ");}   }

The last ingredient to complete the "historical mission", eat 2 bowls to fill!

Five. Finally we need a main.m function, so many hard things to do in the front, in the MAIN.M function is easy!

#import <Foundation/Foundation.h>#import"Custom.h"intMainintargcConst Char*argv[]) {    /*need: Go to a restaurant for dinner*/    //a customer.Custom *CP = [CustomNew]; //Buy RiceCook *CKP = [Restaurant sellfood: the];    [CP EATFOOD:CKP]; //Open Eat    return 0;}

In the MAIN.M function, we need to create a customer object, the customer approached the restaurant (the chef is the boss, the small restaurant!), gave 15 yuan, came a large bowl of leek fried pork loin son!

If you feel that the number of code and objects are more friends, you can write on the paper, a ring with a ring, I want to perform this step, I just need to know the last step. In this case, the customer only needs to pay the restaurant (the chef is the boss ...) You can eat, do not need to know how to make the food!

There is an important point, a friend asked me, about this piece, it sounds like the idea is no problem, but it is to do with the feeling of loopholes! Then I tell you clearly, when you understand or understand the 50%, the rest is to repeat the program, each repeat you have your own experience! No experience you to hit me!).

Beginner friends continue to refuel! More powerful than me Friends of the Great God Please take me to fly ~ ~ ~ Haha!

OK, that's it! If there is a wrong place, also hope to point out. Thank you!

----Niven Moore

Personal summary of the know-how----OC object as a method of continuous, focus is also difficult.

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.