OC basics -- passing parameters of objects between methods, oc --

Source: Internet
Author: User
Tags rounds

OC basics -- passing parameters of objects between methods, oc --

Description:

U. S. soldiers fought for help after they took Hanyang Building 5 bullet clips (5 bullets in each clip) and carried them together

Class:

Soldiers:

Attribute: name (_ name) height (_ height) weight (_ weight)

Action: fire (callForHelp)

Gun:

Attribute: Number of cartridge clips (_ clip) (_ clipNum) brand (_ model)

Behavior: Change the bullet clip (shot)

Cartridge:

Attribute: number of bullets (_ bulletCount)

Behavior: addBullet)

Method description:

Soldier shooting method (fire)

Parameters: Gun and cartridge

Returned value: number of remaining posters

Logic: Use the bullet clip as a parameter to call the gun's shot method to obtain the number of remaining bullet clips

If the remaining number of poll is equal to or greater than 0, call callForHelp)

Shot Method)

Parameter: cartridge

Returned value: the number of clips.

Logic: if the bullet in the cartridge is equal to or greater than 0, call your own cartridge change method to obtain the new cartridge. Otherwise, the bullet in the cartridge is reduced by 1.

Gun-for-magazine (changeClip)

Parameter: cartridge

Returned value: the number of clips.

Logic: set the number of clips-1 (because one has already been used). If there are other clips, call the method for loading the bullets. Otherwise, the system prompts that the bullets have been wiped out.

AddBullet)

Parameter: None

Returned value: the object of the clip.

Logic: if the number of bullets in the clip is = 0, 5 rounds of bullets are loaded and the clip is returned.

Where is the object:

The soldiers shot and received the gun and the bullet clip object. In the method of shooting the object, they passed the bullet clip object into the method of launching the bullet. the method of adding a bullet to obtain the returned value of a bullet clip object is not used.

 

Clip. h:

/* Clip Object Name: Clip attribute: number of bullets (_ bulletCount) behavior: addBullet */# import <Foundation/Foundation. h> @ interface Clip: NSObject {@ public // number of bullets int _ bulletCount;} // attach a bullet to the cartridge-(Clip *) addBullet; @ end

Clip. m:

# Import "Clip. h "@ implementation Clip // you can attach a bullet to the cartridge. Assuming that one bullet Clip is installed at a time (for example, a bullet Clip is changed), the returned object is not actually used.-(Clip *) addBullet {if (_ bulletCount = 0) {_ bulletCount = 5; NSLog ", self-> _ bulletCount);} return self;} @ end

Gun class declaration file Gun. h:

/* Gun thing name: Gun (Gun) attributes: Large Bullet clips (_ bigClip), number of bullet clips (_ clipNum), model Behavior: changeClip) shot */# import <Foundation/Foundation. h> # import "Clip. h "@ interface Gun: NSObject {@ public // Clip * _ bigClip; // Number of poll int _ clipNum; // NSString * _ model of the Gun brand ;} // Method for changing the cartridge-(int) changeClip :( Clip *) newClip; // Method for launching bullets-(int) shot :( Clip *) clip; @ end

Implementation file Gun. m:

# Import "Gun. h "@ implementation Gun // adjust the number of poll returned by the poll object-(int) changeClip :( Clip *) clip {// if you need to change the cartridge, the number of the cartridge has been used-1 _ clipNum-= 1; // if there is a bullet clip, call the method of adding a bullet to the bullet clip if (_ clipNum> 0) {_ bigClip = [clip addBullet]; NSLog (@ "gun number of completed bullets: % I continue to dry ", clip-> _ bulletCount);} else {NSLog (@" gun bullet knocked out ");} return _ clipNum ;} // The number of response clips returned by the client using the gun-fired bullet method-(int) shot :( Clip *) clip {// variable stores the number of clips (you can directly use Attribute members without this variable) int clipNum = _ clipNum; // if no bullet exists in the cartridge, call the change cartridge method if (clip-> _ bulletCount = 0) {NSLog (@ "gun friendly tip: big Brother, if you don't have a bullet, I'm going to play it for you. You'll try it and install it, and then let you know. That's a pleasant decision. "); clipNum = [self changeClip: clip];} // otherwise the bullet-1 else {NSLog (@ "gun bullet minus 1"); clip-> _ bulletCount-= 1;} return clipNum;} @ end

Soldier. h:

/* Soldier thing name: Soldier attributes: name, height, weight behavior: fire, call) */# import <Foundation/Foundation. h> # import "Gun. h "@ interface Soldier: NSObject {@ public NSString * _ name; double _ height; double _ weight;}-(int) fireWithGun :( Gun *) gun andClip :( Clip *) clip;-(void) callForHelp; @ end

Soldier. m:

# Import "Soldier. h "@ implementation Soldier // The number of remaining objects returned by the gunshots and the Clip-(int) fireWithGun :( Gun *) gun andClip :( Clip *) clip {NSLog (@ "soldier sons, sons, kings, eight, and so on ~~~ "); // Pass the clip object to the method int remainClipNum that calls the gun shot = [gun shot: clip]; // call the distress method if (0 = remainClipNum) {[self callForHelp];} return remainClipNum;}-(void) callForHelp {NSLog (@ "soldier: I have no bullets, but I have been playing a lot. You don't know how to scare the baby. Come on. 9. Let's wait for you ");} @ end

Main. m:

# Import <Foundation/Foundation. h> # import "Soldier. h "int main (int argc, const char * argv []) {// Soldier * soldier = [Soldier new]; soldier-> _ height = 2.12; soldier-> _ weight = 80; soldier-> _ name = @ "";
// Clip * clip = [Clip new]; clip-> _ bulletCount = 5; // Gun object gun * Gun = [Gun new]; gun-> _ bigClip = clip; gun-> _ model = @ "hanyangzao"; gun-> _ clipNum = 5; // The soldiers start to hold the racks. // Save the remaining number of cartridge clips. If the number is = 0, the loop int remainClip = 0; while (1) {// call the soldier's shooting method to obtain the number of remaining bullet clips remainClip = [soldier fireWithGun: gun andClip: clip]; if (! RemainClip) {break;
} Return 0 ;}

Output result:

/* Output result: After five rounds, the system prompts that the bullet has been destroyed, and then asks for help. Five bullets are fired in each cycle 23:18:37. 182 object parameter transfer [892: 34503] soldier sons and grandchildren, Wang Babu, are all playing with me and killing chickens ~~~ 23:18:37. 184 object parameter transfer [892: 34503] gun bullet minus 1 23:18:37. 184 object parameter transfer [892: 34503] soldier sons and grandchildren, Wang Babu, are all playing with me and killing chickens ~~~ 23:18:37. 184 object parameter transfer [892: 34503] gun bullet minus 1 23:18:37. 184 object parameter transfer [892: 34503] soldier sons and grandchildren, Wang Babu, are all playing with me and killing chickens ~~~ 23:18:37. 185 object parameter transfer [892: 34503] gun bullet minus 1 23:18:37. 185 object parameter transfer [892: 34503] soldier sons and grandchildren, Wang Babu, are all playing with me and killing chickens ~~~ 23:18:37. 185 object parameter transfer [892: 34503] gun bullet minus 1 23:18:37. 185 object parameter transfer [892: 34503] soldier sons and grandchildren, Wang Babu, are all playing with me and killing chickens ~~~ 23:18:37. 185 object parameter transfer [892: 34503] gun bullet minus 1 23:18:37. 186 object parameter transfer [892: 34503] soldier sons and grandchildren, Wang Babu, are all playing with me and killing chickens ~~~ 23:18:37. 186 object parameter transfer [892: 34503] gun friendly prompt: big Brother, if you don't have bullets, I'm going to pack them for you. You'll fight for a bayonet first. I'll let you know, okay? Let's make a pleasant decision ............... ................ ................ ................ 23:18:37. 217 object parameters passed [892: 34503] gun bullets were destroyed 23:18:37. 217 passing parameters for an object [892: 34503] soldier: I have no bullets, but I am still playing a lot. You don't know how to scare the baby. Come on. 9. Are you waiting for you */

 

 

 

 

 

 

 

 

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.