IOS 2 Setter and getter

Source: Internet
Author: User

Today the main learning of setter and getter use method, according to yesterday's learning content, first we want to create a project, in addition to create a person class!

=================================================================

Create a few instance variables in the person class in Person.h, declaring the setter and getter method "on code" who the DARE code is!

#import <Foundation/Foundation.h>

/*

Instance variable visibility

[Email protected] public visibility : Access is available both inside and outside the class , because external access is also available , so he destroys the encapsulation nature of the class .

[email protected] private visibility : Only the current class is accessible, the subclass is inaccessible, and because the subclass is inaccessible, the inheritance of the class is broken .

[Email protected] protected visibility: The current class and subclasses are accessible . ( is the default ) is also the most suitable for use !

*/

@interface Person: nsobject

{

defining instance variables for the person class

nsstring *_name;// name belongs to string, so use nsstring and take *

nsstring *_sex;// sex belongs to string, so use nsstring and take *

Nsinteger _age;// Age is constant, so with int type, Nsinteger is the constant in OC, no need to take *

cgfloat _weight;// weight data constant, Cgfload equivalent to Fload, so no need to take *

}

Initialize method

-(ID) init;

Custom initialization method form : Initwith + interface part of the parameters for example :

-(ID) initwtihname: (nsstring *) name

Sex: (nsstring *) Sex

Age: (Nsinteger) Age // parameter type not with *

Weight: (cgfloat) weight; // parameter type not with *

Method declaration Greeting

-(void) sayhi;

Set setter method for each instance variable "uniform"

The format of the setter method is with the set header + instance variable (with the underscore , and the first letter capitalized ):(parameter type nsstring) parameter name

-(void) SetName: (nsstring *) name sex: (nsstring *) Sex Age: (Nsinteger) Age Weight: (cgfloat) Weight;

Set setter method "separate" for each instance variable

-(void) SetName: (nsstring *) name;

-(void) Setsex: (nsstring *) sex;

-(void) Setage: (nsinteger) age;

( return value) set ( parameter name) ( parameters type) (argument name )

-(void) Setweight: (cgfloat) weight;

Set the getter method for each instance variable that sets the setter method individually

Getter Format : The return value is the type of the corresponding instance variable , and the method name removes the underscore from the instance variable name

-(NSString *) name;

-(NSString *) sex;

-(Nsinteger) age;

-(cgfloat) weight;

/* It 's best to write setter and getter methods together for example:

-(void) SetName: (NSString *) name;

-(NSString *) name;

*/

@end

=================================================================

In the person.m file, implement the Init method to implement the setter and getter method "on code" to ask who the code is!

#import "Person.h"//here do not misunderstand is in Person.h, this is person.m file

@implementation Person

Initialize the initial assignment for each instance

-(ID) init

{

_name = @ "Jack";

_sex = @ "male";

_age = +;// constant type, so do not use @ "

_weight = 160; //In the same vein as Age

return self;

}

Calling the process of the parent class system initialization

-(ID) initwtihname: (nsstring *) name sex: (nsstring *) Sex Age: (Nsinteger) Age Weight: (cgfloat) Weight

{

Self = [super Init]; calling a method of the parent class

if (self) {// if the method of the parent class is called

_name = name;//_name is the parameter in the top init , name is the name after initwithname

_sex = sex; When the name is passed to the new value in the main function, the _name equals the new name.

_age = age;

_weight = weight;

}

return self;

}

implementation of ========setter (Assignment) and getter ( value) methods ========//

Implementation of the method

-(void) Sayhi

{

NSLog(@ " people have the function of greeting "); Here is a concrete implementation of the methods declared in Person.h !

}

Implement Setter (Assignment ) method for each instance variable "unified"

Implement getter (value ) method for each instance variable "unified"

-(void) SetName: (nsstring *) name sex: (nsstring *) Sex Age: (Nsinteger) Age Weight: (cgfloat) Weight

{

_name = name;

_sex = sex;

_age = age;

_weight = weight;

}

Implement Setter (Assignment ) method for each instance variable "separately"

Note : Each method is implemented with the Person.h declared in the one by one corresponding to the Pro !

-(void) SetName: (nsstring *) name

{

_name = name;// Assigns the value of the incoming name to _name; Maybe a little around, think about it .

}

-(void) Setsex: (nsstring *) Sex

{

_sex = sex;

}

-(void) Setage: (nsinteger) Age

{

_age = age;

}

-(void) Setweight: (cgfloat) weight

{

_weight = weight;

}

Implement getter (value ) method for each instance variable "separately"

Note : Each method is implemented with the Person.h declared in the one by one corresponding to the Pro !

-(nsstring *) The Getter method implementation of name//name

{

return _name;

Return to the top init in the _name below the same method.

}

-(nsstring *) Sex//sex getter method implementation

{

return _sex;

}

-(Nsinteger) Age//age Getter method implementation

{

return _age;

}

-(cgfloat) weight//weight getter method implementation

{

return _weight;

}

Suggested writing format : can be used setter and getter method written in a piece of the way

/*

-(void) SetName: (NSString *) name

{

_name = name;

}

-(NSString *) name

{

return self;

}

*/

@end

=================================================================

In the main function, import the Person.h file, create an object of the person class, and assign a value to the object, and take the value "on the code" who Dare to code!

#import <Foundation/Foundation.h>

#import "Person.h"

int main (int argc, const char * argv[])

{

Create an object for a person

Person *per = [[person alloc] init];

Assign a name to the person's object per

[Per setName:@ "Henry"];

Assigning a gender to an object per

[Per setsex:@ "male"];

Assign age to object per

[Per setage: +];

Assigning a weight to an object per

[Per setweight:];

NSLog (@ "per" name is:%@ ", per); At this point print per is just an address to print, to give the per object a specified printed instance.

If this is the case, the print result is: the name of per is:<person:0x100201e10>

NSLog (@ "per" name :%@ sex :%@ Age :%ld weight :%.2f ", [per Name],[per sex],[per age], [per weight]);

Print Result: per name: Henry Sex: Male Age: Weight: 160.00

re-assigning a value to Object per

[Per setName:@ "Lily" sex:@ "female" Age: weight: +];

NSLog (@ "per" name :%@ sex :%@ Age :%ld weight :%.2f ", [per Name],[per sex],[ per age],[per weight]);

printing results: per name: Lily Sex: Female Age: Weight: 96.00

Per to invoke the method of the person class

[per sayhi];

return 0;

}

OK, OC The second course is finished, I do not know if anyone will read my blog, if you see these friends, can give some advice, because I am also a beginner, we inspire each other.

Today's time is early, I am going to learn the next chapter of the content

IOS 2 Setter and getter

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.