OC basics-encapsulation of member variables, oc-encapsulation of Variables

Source: Internet
Author: User

OC basics-encapsulation of member variables, oc-encapsulation of Variables

# Import <Foundation/Foundation. h> // Date struct typedef struct {int year; int month; int day;} Date; @ interface School: NSObject {// @ public NSString * _ name; NSString * _ address; Date _ createTime;}-(void) introduction; @ end
# Import "School. h "@ implementation School-(void) introduction {NSLog (@" School name: % @, School Address: % @, created: % I, % I, % I ", [self name], [self address], [self createTime]. year, [self createTime]. month, [self createTime]. day) ;}@ end
# Import <Foundation/Foundation. h> # import "School. h "int main (int argc, const char * argv []) {School * s = [School new]; s-> _ name = @" "; s-> _ address = @ "No. XX, XX Road, XX city, XX Province"; s-> _ createTime. year = 2045; s-> _ createTime. month = 13; s-> _ createTime. day = 34; [s introduction]; return 0 ;}

If the member variable is public, the value assigned to the member variable is not subject to our constraints. When the member variable is assigned a value, it is possible to assign some dirty data values to the outside when the member variable can be read and written at will.

Encapsulation:

Shields internal implementation details and only provides common external methods/interfaces.

Benefits: ensures data security and isolates changes

Standard: Hide all content that does not need to be provided externally, hide all attributes, and provide public methods to access them.

Getter-Setter method:

  Setter method:

Purpose: set the value of a member variable.

Format:

1. the setter method must be an object method.

2. There must be no return value

3. It must start with set, and the name of the member variable to be set after set is removed from the underline, and the first letter is capitalized.

4. There must be parameters. The parameter type must be consistent with the type of the member variable to be set, and the parameter name is the name of the member variable, removing the underline.

// The benefit of starting with a line below member variables is that it can distinguish between local variables and member variables-(void) setName: (NSString *) name {// you can add verification code here to verify the passed data _ name = name ;}

Getter method:

Purpose: obtain the value of a member variable.

Format:

1. The getter method must be an object method.

2. There must be a return value, and the return value must be of the same type as the obtained member variable.

3. The method name is the name of the retrieved member variable. Remove the underline.

4. There must be no parameters

        - (NSString* ) name{            return _name;        } 

> A property can only have the getter method and no setter method. This attribute is called a read-only attribute.

> A property can only have the setter method, but does not have the getter method. This property is called a write-only property.

> If both the setter method and the getter method are available, this attribute is called a readable and writable attribute.

> A property can also have no getter or setter. This property is called a private property.

Call the Getter-Setter method:

Date date = {1990, 1, 1}; School * s = [School new]; [s setName: @ "test"]; [s setAddress: @ "tset"]; [s setCreateTime: date]; NSLog (@ "school name: % @", [s name]);

Point Syntax:

> If the getter and setter methods are provided for the attribute, an additional access method is provided for the access attribute.

> The essence of the dot syntax is to call the setter and getter methods.

> The dot syntax is a compiler feature that automatically converts the. syntax to the call of setter and getter methods when the program is translated into binary.

> If the dot syntax is on the left side of the = sign, the compiler automatically converts it to the setter method.

> If the dot syntax is on the right of the = sign, or there is no equal sign, the compiler automatically converts it to the getter method.

S. name = @ "test"; // equivalent to [s setName: @ "test"]; s. address = @ "test"; // equivalent to [s setAddress: @ "test"]; s. createTime = date; // equivalent to [s setCreateTime: date]; NSLog (@ "school name: % @, School Address: % @", s. name, s. address); // equivalent to NSLog (@ "school name: % @, School address: % @", [s name], [s address]);

> The dot syntax is generally used to assign values to member variables. If it is not used to assign values to member variables, it is generally not recommended, but can also be used.

-(Void) test :( int) num {NSLog (@ "test Data % I", num);} s. test; // equivalent to [s test]

 

# Import <Foundation/Foundation. h> // Date struct typedef struct {int year; int month; int day;} Date; @ interface School: NSObject {// @ public NSString * _ name; NSString * _ address; Date _ createTime;}-(void) setName: (NSString *) name;-(void) setAddress: (NSString *) address;-(NSString *) address;-(void) setCreateTime: (Date) date;-(Date) createTime;-(void) introduction;-(void) test; -(void) test: (int) num; @ end # import "School. h "@ implementation School-(void) setName: (NSString *) name {// you can add verification code here to verify the passed data _ name = name ;} -(NSString *) name {return _ name;}-(void) setAddress: (NSString *) address {_ address = address;}-(NSString *) address {return _ address;}-(void) setCreateTime: (Date) date {_ createTime = date;}-(Date) createTime {return _ createTime;}-(void) introduction {NSLog (@ "school name: % @, School Address: % @, founded at: % I, % I, % I", [self name], [self address], [self createTime]. year, [self createTime]. month, [self createTime]. day);} // test point syntax access non-getter-setter method-(void) test {NSLog (@ "");}-(void) test :( int) num {NSLog (@ "test Data % I", num) ;}@ end # import <Foundation/Foundation. h> # import "School. h "int main (int argc, const char * argv []) {// School * s = [School new]; // s-> _ name = @ ""; // s-> _ address = @ "No. XX, XX Road, XX city, XX Province "; // s-> _ createTime. year = 2045; // s-> _ createTime. month = 13; // s-> _ createTime. day = 34; Date date = {1990, 1, 1}; School * s = [School new]; s. name = @ "test"; // equivalent to [s setName: @ "test"]; s. address = @ "test"; // equivalent to [s setAddress: @ "test"]; s. createTime = date; // equivalent to [s setCreateTime: date]; NSLog (@ "school name: % @, School Address: % @", s. name, s. address); // equivalent to NSLog (@ "school name: % @, School address: % @", [s name], [s address]); // point syntax call method without parameters warning: Property access result unused-getters shocould not be used for side effects s. test; // equivalent to [s test]; // point syntax call the method with parameters for compiling directly reports an error s. test: 111; [s introduction]; return 0 ;}

 

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.