"Objective-c Basic Tutorial" Learning Notes (vii)--access method

Source: Internet
Author: User

In the previous blog post, we had a simple "car model" to explain the composite relationship . In today's blog post, we'll go on to the last example, explaining the use of the access (accessor) method . The so-called access method is the method used to read or change an object's properties. If you add a method to change the engine object variable in the car object, it is an access method.

Access methods are divided into two types:setter method and getter method. The setter method, which assigns a value to a variable in an object. The Getter method, which accesses object properties through the object itself.

In Objective-c, the cocoa framework has relevant provisions when defining access methods:

(1). Setter method, named according to the name of the property it is going to change, and preceded by a set prefix . such as: Setengine,settire and so on.

(2). Getter method, it is named after the property name it returns, do not add the get prefix to the getter method name. Because in cocoa, the get prefix has other uses. This usually means that the method will return the value as a pointer to the parameter you passed.

The basic concepts and defined rules are introduced. Next, we'll modify the car model to set the access method for the engine and tire of the car.

First, add the engine and tire access method to the Car object definition first:

1 @interfaceCar:nsobject2     {3Engine *engine;4Tire *tires[4];//Four wheels, defines an array of four numbers. 5     }6-(Engine *) engine; 7-(void) Setengine: (Engine *) Newengine;8-(Tire *) Tireatindex: (int) index;9-(void) Settire: (Tire *) Tire Atindex: (int) index;Ten-(void) Drive; One @end //Car

Because the tire has four subsets, a atindex parameter is added here that represents the first few wheels. Next, implement the storage method defined in CAR:

1 @implementationCar2-(void) Setengine: (Engine *) Newengine3     {4Engine =Newengine;5     }6 7-(Engine *) Engine8     {9         return(engine);Ten     } One  A-(void) Settire: (Tire *) Tire -Atindex: (int) Index -     { the         if(index<0|| Index>3) -         { -NSLog (@"Bad Index (%d) in Settire:atindex", - index); +Exit1); -         }  +Tires[index] =Tire; A     } at  --(Tire *) Tireatindex: (int) Index -     { -         if(index<0|| Index>3) -         { -NSLog (@"Bad Index (%d) in Tireatindex:", in index); -Exit1); to         } +         return(Tires[index]); -     } the  *-(void) drive{ $NSLog (@"%@", engine);Panax NotoginsengNSLog (@"%@", tires[0]); -NSLog (@"%@", tires[1]); theNSLog (@"%@", tires[2]); +NSLog (@"%@", tires[3]); A     } the @end

In order to pass the index parameter does not meet the criteria, so that beyond the array, we made a simple judgment. Finally, we modify main main function:

1 intMainintargcConst Char*argv[])2 {3Car *car = [carNew];4Engine *engine = [engineNew];5 [car setengine:engine];6      for(intI=0;i<4; i++)7     {8Tire *tire = [TireNew];9 [car settire:tire atindex:i];Ten     } One      A [car drive]; -     return 0; -}

At this point, we found that the Init initialization method previously defined in car was removed here. Because car now defines the way to access the engine and the tires variable. Therefore, the Init method is not required to create. Instead, the engine and tire are created directly in the main function.

There is no difference between the running result and the previous:

Through this simple modification example, I hope you have a deeper understanding of how access methods are used in the cocoa framework.

"Objective-c Basic Tutorial" Learning Notes (vii)--access method

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.