Black Horse programmer 06-oc objects and functions

Source: Internet
Author: User

--- Java training, Android training, IOS training, and. Net training. We look forward to communicating with you! ---

Create a car class and use the car class to create an object. The Code is as follows:

1 # import <Foundation/Foundation. h> 2 @ interface car: nsobject 3 {4 // member variable 5 @ public 6 int _ wheels; 7 int _ speed; 8} 9-(void) run; 10 @ end11 12 @ implementation car13-(void) run14 {15 nslog (@ "% d wheels, the speed is % ikm/h of the car to run", _ wheels, _ speed); 16} 17 @ end18 int main (INT argc, const char * argv []) 19 {20 car * car1 = [Car new]; 21 car1-> _ wheels = 4; 22 car1-> _ speed = 250; 23 24 [car1 run]; 25 return 0; 26}

The output result of the above Code is: four wheels, and the car with a speed of 250 km/h is running.

In the above Code, we add a function to analyze the changes in the output results:

1. Add three functions: Test, test1, and Test2.

 1 void test(int w, int s) 2 { 3     w = 20; 4     s = 200; 5 } 6  7 void test1(Car *newCar) 8 { 9     newCar -> _wheels = 5;10 }11 12 void test2(Car *newCar)13 {14     Car *car2 = [Car new];15     car2 -> _wheels = 5;16     car2 -> _speed = 300;17     newCar = car2;18     newCar -> _wheels = 6;19 }

2. Analysis

(1) When the main function calls the test function, the real parameter _ wheels = 4, _ speed = 250 is passed to the test function, but the value of W and S is changed, the value of _ wheels and _ speed is not changed. Therefore, the output result is: four wheels and the car with a speed of 250 km/h is running.

(2) car1 calls the test1 function. car1 is passed to the test1 function as a pointer variable and belongs to the address. In the test1 function, the value of car1-> _ wheels = 5 and _ wheels is changed. So the output result is: four wheels, and the car with a speed of 250 km/h is running.

(3) Test2 function, the function parameter is a car class pointer variable. In this function, a new object is created using the car class, and the address of the new object is assigned to car2. In this case, car2 points to the new object; next, car2 assigns values to the member variables of the new object.

When car1 calls the Test2 function, it executes the 17th line code and the result is: car1 = car2, which changes the direction of car1, that is, car1 also points to the object pointed to by car2, then, car-> _ wheels = 6 is used to assign a value to the new object pointed to by car2. From start to end, no assignment operation was performed on the objects originally pointed to by car1. The output result is four wheels and the car with a speed of 250 km/h is running.

Note: because an object assigns an address to a pointer variable, we are used to calling this pointer variable pointing to an object as an object.

Black Horse programmer 06-oc objects and functions

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.