@fiy-fish
2015-05-18T15:24:11.000000Z
字数 873
阅读 1591
Objective-c
#import <Foundation/Foundation.h>@interface Student : NSObject{int _score;}//设值方法 setter方法 作用是给实例变量赋值//标准的setter方法的写法//-(void)set实例变量名:(类型)形参名;//: 表示参数//参数的类型用()括起来 写在:后面//参数名接在类型后面//方法名是 setScore://也被叫做 方法签名- (void)setScore:(int)s;//getter 取值//OC里面的getter方法标准写法-(int)score;@end
// Student.m// setter和getter//// Created by Aaron on 15/5/18.// Copyright (c) 2015年 Aaron. All rights reserved.//#import "Student.h"@implementation Student-(void)setScore:(int)s{_score = s;}-(void)printMe{NSLog(@"我是一个学生,分数是%d",_score);}-(int)score{return _score;}@end
// main.m// setter和getter//// Created by Aaron on 15/5/18.// Copyright (c) 2015年 Aaron. All rights reserved.//#import <Foundation/Foundation.h>#import "Student.h"int main(int argc, const char * argv[]){@autoreleasepool {Student *std = [Student alloc];[std setScore:10000];[std printMe];//对于有返回值的方法,调用方法的表达式就是其返回值int score = [std score];NSLog(@"----%d",score);}return 0;}
