Oc-16-set,get method

Source: Internet
Author: User

S.h

#import<Foundation/Foundation.h>/** Solution: 1. Do not @public decoration 2. We object has two operations to access and set member variables 1> set Value p->age = ten; 2> Access value NSLog (@ "%d", p->age); Now there are two ways to set the value and access the value 1> the format of the member variable Convention:-(void) Set member variable name (minus underscore, first capitalization):(member variable data type) member variable name (remove underscore);  -(void) Setage: (int) age; 2> the format of the Access member variable Convention:-(member variable data type) member variable name (minus underscore); -(int) age; *///. h files are exposed outside,. m files are hidden. @interfacestudent:nsobject{int_age;//age, the member variable is underlined, and the local variable is not underlined. NSString *_name;//name}//provide age-setting and access Methods- (void) Setage: (int) age;- (int) age;//methods for setting and accessing names- (void) SetName: (NSString *) name;-(NSString *) name;@end

S.m

#import "Student.h"@implementationStudent//provide age-setting and access Methods- (void) Setage: (int) age{if(Age >0&& Age < -) {_age=Age ; }Else{//Age =;_age = -; }}- (int) age{return_age;}//methods for setting and accessing names//compares whether two strings are the same-(BOOL) isequaltostring: (NSString *) astring;- (void) SetName: (NSString *) name{if([name length] >2) {        if([Name isequaltostring:@"D Major"] || [Name isequaltostring:@"method in D major"]) {_name=@"Anonymous"; }Else{_name=name; }    }    Else{NSLog (@"Please re-enter!"); }    }-(NSString *) name{return_name;}@end

Oc-16-set,get method

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.