[Learning notes] [OC language] set Method and get method, learning notes ocsetget

Source: Internet
Author: User

[Learning notes] [OC language] set Method and get method, learning notes ocsetget

1. Use Cases of the set and get Methods
@ Public members can be assigned a value at will. The set and get methods should be used to manage member access (such as airport security check and faucet filtering to filter out unreasonable items ), for example, the botnet life cannot be negative.
2. set Method
Function: Used to set member variables. Some unreasonable values can be filtered out in the method.
Naming rules:
All methods start with set and follow the name of the member variable. The first letter of the member variable name must be capitalized.
The parameter name must not be the same as the member variable name.
3. get Method
Purpose: return the member variables in the object.
Naming Convention: The get method name is generally the same as the member variable name.
4. Naming rules for member variables
All member variables start with underscore _
The name can be separated from the get method.
It can be separated from other local variables. The variable starting with an underscore must be a member variable.
5. Sample Code

1 # import <Foundation/Foundation. h> 2 // declare 3 @ interface Car: NSObject 4 {5 int _ wheels; // Number of wheels 6} 7/* set Method */8-(void) setWheels :( int) wheels; 9/* get Method */10-(int) wheels; 11 @ end12 13 @ implementation Car14/set method implementation 15-(void) setWheels :( int) wheels16 {17 // filter the number of wheels passed in outside 18 if (wheels <= 0) 19 {20 wheels = 1; 21} 22 23 _ wheels = wheels; 24} 25 26 // get method implementation 27-(int) wheels28 {29 return _ wheels; 30} 31 @ end
1 # import <Foundation/Foundation. h> 2 3 @ interface Student: NSObject 4 {5 // do not use @ public 6 // @ public 7 int age as far as possible; 8 9 // @ public10 // readonly: only allow external access to my no, do not allow external modification to my no11 int no; // you only need to provide the get Method 12} 13 14 // 15/* 16 set Method 17 1. purpose: provide a method to set member variable values for the outside world. You can filter parameters in the method by 18 2. naming rules: 19 1> the method name must start with set 20 2> the name of the member variable following the set, the first letter of the member variable must be capitalized 21 3> the return value must be void22 4> be sure to receive a parameter, and the parameter type is the same as the member variable type 23 5> The Name Of The form parameter cannot be the same as the member variable name 24 */25-(void) setAge :( int) newAge; 26 27/* 28 get method 29 1. purpose: return 30 member variables in the object. 2. naming rules: 31 1> there must be a returned value, the return value type must be consistent with the member variable type 32 2> the method name is the same as the member variable name 33 3> you do not need to receive any parameter 34 */35-(int) age; 36 37-(void) study; 38 39 @ end40 41 @ implementation Student42 43 // implementation of the set Method 44-(void) setAge :( int) newAge45 {46 // filter the passed parameters. 47 if (newAge <= 0) 48 {49 newAge = 1; 50} 51 52 age = newAge; 53} 54 55-(int) age56 {57 return age; 58} 59 60-(void) study61 {62 NSLog (@ "% d-year-old student learning ", age); 63} 64 65 @ end66 67 int main () 68 {69 Student * stu = [Student new]; 70 // stu-> age =-10; 71 72 // stu-> age = 10; 73 74 [stu setAge: 10]; 75 76 77 NSLog (@ "the student's age is % d ", [stu age]); 78 79 // [stu study]; 80 81 82 return 0; 83}

 

 

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.