Wei iOS Basic learning Note Nine OC language Basics -04 getter and Setter methods

Source: Internet
Author: User

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{3 Engine *Engine4 Tire *tires[4];//Four wheels, define an array of four numbers.} 6-(Engine *) engine;  7-(void) Setengine: (Engine *  8-(Tire *) Tireatindex: (int ) index;  9-(void) Settire: (Tire *) Tire atindex: (int) index; 10-(void11  @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{4 engine =Newengine;5}67-(Engine *) engine8{9Return(engine);10}1112-(void) Settire: (Tire *) TireAtindex: (Int) Index14{15if (index<0 | | Index>3)16{NSLog (@"Bad index (%d) in Settire:atindex",18index);Exit (1);20}Tires[index] =Tire22}23-(Tire *) Tireatindex: (Int) Index25{26if (index<0 | | Index>3)27{NSLog (@"Bad index (%d) in Tireatindex:",29index);Exit (1);31}32Return(Tires[index]);33}3435-(void) drive{NSLog (@"%@", engine);Panax Notoginseng NSLog (@"%@", tires[038 NSLog (@ "%@ ", Tires[1");  NSLog (@ "%@240 NSLog (@ "%@ ", Tires[3");  }42  @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:

1int main (int argc,Constchar *Argv[])2{3 Car *car = [carNew];4 engine *engine = [engineNew];5[Car setengine:engine]; 6 for (int I=0;i<4;i++)  7  { 8 Tire *tire = [Tire new  [car settire:tire atindex:i];10 }11 12  [car Drive]; 13 return 0; 14}             

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:

Wei iOS Basic learning Note Nine OC language Basics -04 getter and Setter methods

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.