- ------<a href="http://www.itheima.com" target="blank">Java培训、Android培训、iOS培训、.Net培训</a>、期待与您交流! -------
/*
类:一类事物共同特征和行为的抽象
对象:类的具体的个数
2,类的定义:分为两部分
1)类的声明(规定当前类的:类名,属性、行为)
@interface 类名:副类名
{定义类的属性}
类的行为
@end
2)类的实现(实现行为)
@implementation 类名
行为的具体实现
@end
*/
/*
在oc中已经存在的类,如何创建一个对象
回顾c结构体,结构体如何创建结构体变量
struct student stu;//stu就是结构体变量
struct student *pstu; //pstu=&stu
结构体指针访问 结构体变量的成员变量值
在oc中创建对象
[Car new];
//做了三件事
//1)向计算机申请内存空间
//2)给类中的每一个成员初始化值
//3)返回新申请的空间的首地址
//理解方式1
//定义了一个Car类型的指针变量
//指针变量指向新申请的内存空间
//理解方式2
//用Car类实例化了一个实例对象,对象名称是p
Car *p=[Car new];
*/
#import <Foundation/Foundation.h>
//车的类的声明
@interface Car : NSObject
{//类的属性
@public
int lunzi;
//char color;
NSString*color;
int speed;
}
@end
//类的实现
@implementation Car
@end
int main(int argc,const char * argv[]) {
@autoreleasepool {
//创建Car类型的对象car1
Car *car1=[Car new];
//创建一个对象实质还是指针 (用指针间接访问)
car1->lunzi = 3;
car1->speed = 150;
car1->color =@"蓝色";
//查看车的信息
NSLog(@"轮子:%d,速度:%d,颜色:%@",car1->lunzi,car1->speed,car1->color);
}
return 0;
}