objective-c 的私有方法
接口定义:
#import <Foundation/Foundation.h> @interface Fraction: NSObject{ int numerator; int denominator; } -(void) print; -(void) setNumerator:(int) n; -(void) setDenominator:(int) d; -(int) numerator; -(int) denominator; @end
实现定义:
#import "Fraction.h" @interface Fraction(Private) -(int) other; @end @implementation Fraction -(int) other{ return 9; } -(void) print{ printf("%i/%i",numerator,denominator); } -(void) setNumerator:(int) n{ numerator = n; } -(void) setDenominator:(int) d{ denominator = d; } -(int) denominator{ return denominator; } -(int) numerator{ int cc = [self other]; printf("this is %d",cc); return numerator + cc; }
实现里面要想写私有方法,就在里面要写一个和接口一样名字的接口,
@interfaceFraction(Private)
other方法就是私有方法了,[selfother]和java的this.other()差不多;
写main函数测试
#import "Fraction.h" int main(int argc,const char *argv[]){ Fraction *frac = [[Fraction alloc] init]; [frac setNumerator:1]; [frac setDenominator:3]; printf("the fraction is :"); [frac print]; printf("\n"); [frac numerator]; [frac release]; return 0; }
fracnumerator就类似java的frac.numerator(getter方法),调用后就会打印私有方法的9啦。。
相关推荐
fort0 2020-05-16
81570790 2020-04-16
86540698 2020-04-08
zhoutaifeng 2020-03-07
好好学习天天 2020-03-06
InterestSoul 2020-02-17
82467413 2019-12-31
84407518 2012-07-16
81570790 2013-07-01
83254851 2013-08-29
84334052 2019-11-19
86540698 2019-11-19
84467715 2011-07-25
81731290 2014-03-22
85437811 2014-03-22
dahuichen 2019-10-25
ObjectiveC 2011-09-20
80530297 2015-04-14
80530297 2015-04-20