TA的每日心情 | 汗 2024-10-15 10:05 |
---|
签到天数: 372 天 [LV.9]以坛为家II
|
Dog.h
- #import <Foundation/Foundation.h>
- @interface Dog : NSObjec
- {
- int _age;
- }
- @property (readwrite) int age;//h文件中申明setter与getter方法
- - (void) setAge:(int)newAge;
- - (void) age;
- @end
复制代码
Dog.m- #import "Dog.h"
- @implementation Dog
- @systhesize age = _age;//m文件中生成setter与getter方法,变量与函数名相同时亦可写成@synthesize age;
- - (void) setAge:(int)newAge{
- _age = newAge;
- }
- - (int)age{
- return _age;
- }
- @end
复制代码
main.m- #import<Foundation/Foundation.h>
- #import "Dog.h"
- int main(int argc,const char * argv[])
- {
- Dog * dog = [[Dog alloc] init];
- [dog setAge:10];
- int dogAge = [dog age];
- printf("The dos age is %d",dogAge);
-
- dog.age = 20;
- dogAge = dog.age;
- printf("The new dog age is %d",dogAge);
-
- return 0;
- }
复制代码 |
|