iOS OC01_类和对象
2015-07-20 16:27
531 查看
.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;
}
//
// 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;
}
相关文章推荐
- iOS编码规范文档
- 调试iOS 已经发布代码 Crash 文件分析出出错对应代码
- [iOS]14个Xcode中常用的快捷键操作
- 免费的iOS企业证书打包服务
- iOS给一张矩形图片剪切成圆形图片
- IOS NSNotification Center 通知中心的使用
- iOS9适配小结
- IOS NSNotification Center 通知中心的使用
- iOS 开发:设计模式
- IOS:MD5加密
- IOS开发笔记——用PushMeBaby测试Push推送
- iOS开发系列--通讯录、蓝牙、内购、GameCenter、iCloud、Passbook系统服务开发汇总
- iOS 代理传值
- iOS开发系列--音频播放、录音、视频播放、拍照、视频录制
- iOS开发系列--通知与消息机制
- 自定义iOS7导航栏背景,标题和返回按钮文字颜色
- iOS开发系列--地图与定位
- iOS TPKeyboardAvoiding第三方库使用
- ios推送证书的制作
- iOS多线程开发(四)---线程同步