1、枚举:Sex为枚举类型名称;k代表常量
typedef enum{
kSexMan,
kSexWoman
} Sex;
2、结构体:变量使用结构体
typedef struct{
int year;
int month;
int day;
} Date;
//声明后在interface中的定义
@interface Student:NSObject
{
Sex sex; //性别
Date birthday; //生日
char *name; //名字
}
//枚举和结构体访问,结构体内部成员使用小数点访问
int main()
{
Student *s = [Student new];
s ->sex = kSexMan;
s ->name = "Henry";
s ->birthday.year = 2017;
s ->birthday.month = 1;
s ->birthday.day = 1;
或
Date d = {2017,1,1};
s ->birthday = d;
}
2、将对象作为一个属性进行传递
2、将对象作为一个属性进行传递
创建一个狗类,在人类中创建一个狗的成员变量
在main方法中进行调用
@interface Dog:NSObject
@end
@interface Person:NSObject
Dog *dog;
@end
int main()
{
Person *p = [Person new];
Dog *d = [Dog new];
p ->dog = d;
}
3、练习:1、使用同一个类创建多个对象进行比较
创建一个Car类,C1的速度和C2的速度进行比较
- (void)compareWithOther:(Car *)otherCar
{
return speed - otherCar ->speed;
}
int main()
{
[c1 compareWithOther:c2];
}
2、方法名后面要写注释,这是习惯