.h
//
// Student.h
// OC01_类和对象
//
// Created by dllo on 15/7/15.
// Copyright (c) 2015年 zhozhicheng. All rights reserved.
//
#import <Foundation/Foundation.h>
// @interface 接口文件,一个类的开始
// Student 是当前的类名
// NSObject是继承的父类
// 类到@end才结束
@interface Student : NSObject
// 特征
{
//成员变量,或实例变量
@public
// 成员变量的可见度
NSString *_stuName;
NSString *_stuSex;
NSInteger _stuAge;
CGFloat _stuScore;
NSString *_stuHobby;
}
//行为
-(void)sayHi;
-(void)eat;
-(void)play;
@end
// 文件名和类名没有任何关系,但是为了方便对文件里的类进行管理,所以让文件名和类型相同
// 文件中可以有多个类,但是还是方便管理的原则,一个文件中只写一个类
.m
//
// Student.m
// OC01_类和对象
//
// Created by dllo on 15/7/15.
// Copyright (c) 2015年 zhozhicheng. All rights reserved.
//
#import "Student.h"
// 对应实现文件
@implementation Student
-(void)sayHi{
NSLog(@"你好");
}
-(void)eat{
NSLog(@"吃饭吧");
}
-(void)play{
NSLog(@"想玩");
}
// 重写继承过来的init方法
-(id)init
{
_stuName = @"何岸";
_stuSex = @"女";
_stuHobby = @"男";
_stuAge = 20;
_stuScore =100;
return self;
}
@end
main
//
// main.m
// OC01_类和对象
//
// Created by dllo on 15/7/15.
// Copyright (c) 2015年 zhozhicheng. All rights reserved.
//
#import <Foundation/Foundation.h>
// 先引头文件
#import "Audio car.h"
#import "Student.h"
int main(int argc, const char * argv[]) {
// // 打印
// NSLog(@"刘山山");
// //整型
// NSInteger i = 100;
// NSLog(@"%ld",i);
// // 浮点型
// CGFloat f = 3.14;
// NSLog(@"%g",f);
// // 字符串
// // OC字符串放中文没有问题
// NSString *str = @"刘山山";
// NSLog(@"%@",str);
// // OC里的数组
// NSArray *arr = @[@"1",@"2"];
// NSLog(@"%@",arr);
// for (NSInteger i = 0; i < 2; i++) {
// NSLog(@"%@",arr[i]);
// }
// 创建一个对象
// 创建对象需要两步,
// 1.需要给对象开辟空间,开辟堆空间的内存
// Student *stu = [Student alloc];
//
// // 2.对象内存开辟之后,需要对对象进行初始化设置
// stu =[stu init];
// 把两部合并
// Student *stu = [[Student alloc] init];
// // 通过对象来调用行为
// [stu sayHi];
// // 操作成员变量
// // 通过对象->来访问自己的成员变量
// NSLog(@"%@",stu->_stuName);
// stu->_stuAge = 100;
// NSLog(@"%ld",stu->_stuAge);
//
// //改姓名
// stu->_stuName =@"杨林";
// NSLog(@"%@",stu->_stuName);
// stu->_stuName =@"刘山山";
// NSLog(@"%@",stu->_stuName);
//通过Audio的类,创建对象,并对对象的成员变量进行修改
// Audio_car *car =[[Audio_car alloc] init];
// [car knock];
// car->_carCharge =@"10000";
// NSLog(@"%@",car->_carCharge);
Student *stu =[[Student alloc]init ];
NSLog(@"%@",stu->_stuName);
Audio_car *car=[[Audio_car alloc] init];
NSLog(@"%ld",car->_carCharge);
// 使用sayHi方法
[car sayHi];
return 0;
}