- #import <foundation/foundation.h>
- //学生类声明
- @interface CZStudent : NSObject
- @property NSString * name; // 姓名
- @property int age; // 年龄
- @property float scores;// 成绩
- @end
- @implementation CZStudent
- // 此处可以自定义构造方法,改变成员变量默认初始化的值
- @end
- int main(){
- //创建5个对象
- CZStudent * stu1 = [CZStudent new];
- CZStudent * stu2 = [CZStudent new];
- CZStudent * stu3 = [CZStudent new];
- CZStudent * stu4 = [CZStudent new];
- CZStudent * stu5 = [CZStudent new];
- stu1.name = @"小明";
- stu1.age = 10;
- stu1.scores = 99.9;
- //……
- //以此类推 可以给每个对象的成员变量赋值
- //打印结果
- NSLog(@"name=%@ age = %d scores = %.2f%",stu1.name,stu1.age,stu1.scores);
- return 0;
- }
复制代码 |