Simple value transfer between classes

Source: Internet
Author: User

Main. m

# Import <Foundation/Foundation. h> # import "car. H "# import" engine. H "# import" lamp. H "/* design the following categories: Car custom initialization method, the initialization method is passed into the engine object and headlight object. When the car starts, it will call the engine to turn, the lights turn on, when the car stops calling the engine to stop turning, the lights turn out. */INT main (INT argc, const char * argv []) {// create an engine object engine * Engine = [[engine alloc] initwithmodel: @ "1234567" withcapacity: 3.14]; // create a lamp * lamp = [[LAMP alloc] initwithwattage: 60.0]; // create a car * car = [[Car alloc] initwithengine: engine withlamp: Lamp]; [Car setname: @ "Lamborghini" withlicence: @ "8888888"]; int s; while (1) {nslog (@ "Enter the command (1: start 2: Stop): "); scanf (" % d ", & S); If (S = 1) {[car run];} else if (S = 2) {[car stop];} else {nslog (@ "illegal input") ;}} return 0 ;}

Car. h

# Import <Foundation/Foundation. h> # import "engine. H "# import" lamp. H "@ interface car: nsobject {nsstring * _ name; // vehicle Name nsstring * _ licence; // license plate number engine * _ engine; // engine lamp * _ lamp; // headlight} // custom initialization method-(ID) initwithengine :( engine *) engine withlamp :( lamp *) lamp;-(void) setname :( nsstring *) name withlicence :( nsstring *) licence; // vehicle start-(void) run; // vehicle stop-(void) stop;

Car. m

# Import "car. H "@ implementation car // custom initialization method-(ID) initwithengine :( engine *) engine withlamp :( lamp *) lamp {self = [Super init]; If (self! = Nil) {_ engine = engine; _ lamp = lamp;} return self;}-(void) setname :( nsstring *) Name withlicence :( nsstring *) licence {_ name = Name; _ licence = licence;} // vehicle start-(void) run {nslog (@ "license plate number: % @ ", _ licence, _ name); [_ engine turn]; [_ lamp light];} // vehicle stop-(void) Stop {nslog (@ "license plate number: % @ ", _ licence, _ name); [_ engine stopturn]; [_ lamp dark];}
Engine. h

# Import <Foundation/Foundation. h> @ Interface Engine: nsobject {nsstring * _ model; // float _ capacity; // displacement} // custom initialization method-(ID) initwithmodel :( nsstring *) model withcapacity :( float) capacity; // turn-(void) turn; // stop-(void) stopturn;

Engine. m

// Custom initialization method-(ID) initwithmodel :( nsstring *) model withcapacity :( float) Capacity {self = [Super init]; If (self! = Nil) {_ model = model; _ capacity = capacity;} return self;} // turn-(void) Turn {nslog (@ "model: % @ displacement: %. 2f engine turned ", _ model, _ capacity);} // stop rotation-(void) stopturn {nslog (@" model: % @ displacement: %. the 2f engine stops rotating ", _ model, _ capacity );}

Lamp. h

@ Interface lamp: nsobject {float _ wattage; // Watt} // custom initialization method-(ID) initwithwattage :( float) wattage; // light-(void) light; // turn off the light-(void) dark;

Lamp. m

// Custom initialization method-(ID) initwithwattage :( float) wattage {self = [Super init]; If (self! = Nil) {_ wattage = wattage;} return self;} // light-(void) Light {nslog (@ "Watt: %. 2f lights on ", _ wattage);} // turn off the light-(void) Dark {nslog (@" wattage: %. 2f lights out ", _ wattage );}



Simple value transfer between classes

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.