题目要求: 定义一个Person的类,并包含姓名,年龄,身高,体重,性别,设定并打印出年龄的值.
一.传统setter方法和getter方法
二.@property 增强型的使用
- //注:为使使代码在一块清晰可见,我将.h和.m中的代码都放在main.m文件中
- #import <Foundation/Foundation.h>
- @interface Person : NSObject
- //通过@property增强型来实现set方法和get方法的声明和实现
- @property NSString* name;
- @property int age,sex;
- @property float height,weight;
- @end
- #import "Person.h"
- @implementation Person
- @end
- #import <Foundation/Foundation.h>
- #import "Person.h"
- int main(int argc, const char * argv[]) {
- @autoreleasepool {
-
- Person *p = [Person new];
-
- //通过点语法为实例变量赋值并打印
- p.age = 18;
- NSLog(@"age = %d",p.age);
-
- }
- return 0;
- }
复制代码
|