Dark Horse Programmer---objective-c basic learning---get and set methods

Source: Internet
Author: User



Get and set methods in Objective-c






Set method



1. Function: Provide a method to set the value of member variables to the outside world, can be in the method to face the parameters of the corresponding filter



2. Naming conventions:



1> method name must begin with set



2> set followed by the name of the member variable, the first letter of the member variable must be capitalized



3> return value must be void



4> must receive a parameter, and the parameter type is consistent with the member variable type



The name of the 5> parameter cannot be the same as the member variable name


-(void) Setage: (int) newage;





Get method



1. Function: Returns the member variable inside the object



2. Naming conventions:



1> must have a return value, and the return value type must be the same as the member variable type



The 2> method name is the same as the member variable name



3> does not need to receive any parameters


- (int)age;

- (void)study;


Defines a student class that has a member variable, age, and the corresponding Get\set method.


1.student.h
1 #import <Foundation / Foundation.h>
  2 
  3 @interface Student: NSObject
  4
  5 {
  6 // Don't use @public as a member variable
  7 int age;
  8 }
  9 
10-(void) setAge: (int) newAge;
11-(int) age;
12
13 @end 


1> defines a member variable age in line 7th, which is @protected permission, so the outside world cannot access it directly



2> the Set method and the Get method for the age variable in line 10th and 11, respectively


2.student.m
 
 
1 #import "Student.h"
 2 
 3 @implementation Student
 4 
 5 - (void)setAge:(int)newAge {
 6     age = newAge;
 7 }
 8 
 9 - (int)age {
10     return age;
11 }
12 
13 @end


1> implements the Set method on line 5th



2> implemented the Get method on line 9th


3.main.m


Put the well-defined student class in the main function


1 #import <Foundation / Foundation.h>
  2 #import "Student.h"
  3
  4 int main (int argc, const char * argv [])
  5 {
  6 @autoreleasepool {
  7 Student * stu = [[Student alloc] init];
  8         
  9 // Set the value of age
10 [stu setAge: 10];
11
12 // Get the value of age
13 int age = [stu age];
14
15 NSLog (@ "age is% i", age);
16
17 [stu release];
18}
19 return 0;
20} 


1> header file containing student in line 2



2> create student object on line 7th, release student object on line 17th



3> call the Set method on line 10th to set the value of age



4> call the Get method on line 13th to get the value of age



5> outputs the value of age on line 15th, and the output is as follows:


----------57.345 a. Out [2643:  707isten


This is a simple use of the OC Traditional get method and set method.



Dark Horse Programmer---objective-c basic learning---get and set 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.