iOS前期OC训练OC_01类和对象
2015-07-16 20:45
501 查看
//
// main.m
// OC01_类和对象
//
// Created by dllo on 15/7/15.
// Copyright (c) 2015年 Clare. All rights reserved.
//
#import <Foundation/Foundation.h>
// 先引头文件,需要几个类就引几个头文件
#import "Student.h"
#import "AudiCar.h"
#import "Cellphone.h"
int main(int argc,constchar * 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);
// NSLog(@"%ld", stu->_stuAge);
// NSLog(@"%@", stu->_stuSex);
// NSLog(@"%@", stu->_stuHobby);
// NSLog(@"%lg", stu->_stuScore);
//
// stu->_stuAge = 100;
// NSLog(@"%ld", stu->_stuAge);
//
// // 改姓名
// stu->_stuName = @"杨林";
// NSLog(@"%@", stu->_stuName);
// stu->_stuName = @"刘珊珊";
// NSLog(@"%@", stu->_stuName);
// // 通过手机类,创建对象,并且对对象的成员变量进行修改
// Cellphone *phone = [[Cellphone alloc] init];
//
// phone->_phoneBrand = @"Mi";
// phone->_phoneColor = @"Black";
// phone->_phoneModel = @"红米";
// phone->_phonePrice = 700;
// phone->_phoneSize = @"5.7寸";
// NSLog(@"%@", phone->_phoneBrand);
// NSLog(@"%@", phone->_phoneColor);
// NSLog(@"%@", phone->_phoneModel);
// NSLog(@"%lg元", phone->_phonePrice);
// NSLog(@"%@", phone->_phoneSize);
//
// Student *stu = [[Student alloc] init];
// NSLog(@"%@", stu->_stuName);
//
// Student *stu1 = [[Student alloc] init];
// // self 就是自己
Cellphone *phone = [[Cellphonealloc]init];
NSLog(@"%g", phone->_phonePrice);
//
使用sayHi方法
[phonesayHi];
phone->_phoneModel =@"你好";
[phonesayHi];
return0;
}
//
// Cellphone.h
// OC01_类和对象
//
// Created by dllo on 15/7/15.
// Copyright (c) 2015年 Clare. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface Cellphone :NSObject
// 特征
{
@public//公共的,所以都能用
// @protected // 保护,当前类和子类能用
// @private // 私有,只有当前类能用
NSString *_phoneBrand;
NSString *_phoneModel;
NSString *_phoneColor;
NSString *_phoneSize;
CGFloat _phonePrice;
}
// 行为
- (void)call; //打电话
- (void)surfOnline;//上网
- (void)TFBoy; //贴膜
// 写一个用来打印全部信息的功能
- (void)sayHi;
@end
//
// Cellphone.m
// OC01_类和对象
//
// Created by dllo on 15/7/15.
// Copyright (c) 2015年 Clare. All rights reserved.
//
#import "Cellphone.h"
@implementation Cellphone
// 打电话
- (void)call{
NSLog(@"打电话");
}
// 上网
- (void)surfOnline{
NSLog(@"上网");
}
// 贴膜
- (void)TFBoy{
NSLog(@"贴膜");
}
// 重写电话的初始化方法
- (id)init
{
_phoneColor =
@"白色";
_phonePrice =
1999;
_phoneModel =
@"大屏";
_phoneBrand =
@"华为";
_phoneSize =
@"4.7寸";
return
self;
}
- (void)sayHi
{
NSLog(@"%@, %@, %@, %@, %g",_phoneBrand,_phoneModel,_phoneColor,_phoneSize,_phonePrice);
}
@end
//
// AudiCar.h
// OC01_类和对象
//
// Created by dllo on 15/7/15.
// Copyright (c) 2015年 Clare. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface AudiCar :NSObject
{
@public
NSString *_carName;
NSString *_carModel;
CGFloat _carPrice;
NSInteger _carTyre;
NSInteger *_carColor;
}
- (void)run;
- (void)refuel;
- (void)knock;
@end
//
// AudiCar.m
// OC01_类和对象
//
// Created by dllo on 15/7/15.
// Copyright (c) 2015年 Clare. All rights reserved.
//
#import "AudiCar.h"
@implementation AudiCar
- (void)run{
NSLog(@"飙车");
}
- (void)refuel{
NSLog(@"加油");
}
- (void)knock{
NSLog(@"撞车");
}
@end
//
// Student.h
// OC01_类和对象
//
// Created by dllo on 15/7/15.
// Copyright (c) 2015年 Clare. All rights reserved.
//
#import <Foundation/Foundation.h>
// @interface 接口文件,一个类的开始
// Student是当前的类名
// NSObject是继承的父类
// 类到@end才结束
@interface Student :NSObject
// 特征
{
@public
// @public成员变量的可见度
//成员变量,或实例变量
NSString *_stuName; //姓名
NSString *_stuSex; //性别
NSInteger _stuAge; //年龄
CGFloat _stuScore; //成绩
NSString *_stuHobby;//爱好
}
// 行为
// 打招呼
- (void)sayHi;
// 吃
- (void)eat;
// 玩
- (void)play;
@end
// 文件名和类名没有任何关系,但是为了方便对文件里的类进行管理,所以让文件名和类名相同
// 文件中可以写多个类,但是还是为了方便管理的原则,一个文件只写一个类
// 一个文件可以有多个类,但是为了方便管理,一个文件中只写一个类
//
// Student.m
// OC01_类和对象
//
// Created by dllo on 15/7/15.
// Copyright (c) 2015年 Clare. All rights reserved.
//
#import "Student.h"
// 对应的实现文件
@implementation Student
// 打招呼
- (void)sayHi{
NSLog(@"你好");
}
// 吃
- (void)eat{
NSLog(@"吃饭吧!");
}
// 玩
- (void)play{
NSLog(@"想玩");
}
// 重写继承过来的init方法
- (id)init //id == void *即无类型指针
{
_stuName =@"何岸";
_stuSex =@"女";
_stuHobby =
@"男";
_stuAge =20;
_stuScore =
100;
return
self;
}
@end
// main.m
// OC01_类和对象
//
// Created by dllo on 15/7/15.
// Copyright (c) 2015年 Clare. All rights reserved.
//
#import <Foundation/Foundation.h>
// 先引头文件,需要几个类就引几个头文件
#import "Student.h"
#import "AudiCar.h"
#import "Cellphone.h"
int main(int argc,constchar * 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);
// NSLog(@"%ld", stu->_stuAge);
// NSLog(@"%@", stu->_stuSex);
// NSLog(@"%@", stu->_stuHobby);
// NSLog(@"%lg", stu->_stuScore);
//
// stu->_stuAge = 100;
// NSLog(@"%ld", stu->_stuAge);
//
// // 改姓名
// stu->_stuName = @"杨林";
// NSLog(@"%@", stu->_stuName);
// stu->_stuName = @"刘珊珊";
// NSLog(@"%@", stu->_stuName);
// // 通过手机类,创建对象,并且对对象的成员变量进行修改
// Cellphone *phone = [[Cellphone alloc] init];
//
// phone->_phoneBrand = @"Mi";
// phone->_phoneColor = @"Black";
// phone->_phoneModel = @"红米";
// phone->_phonePrice = 700;
// phone->_phoneSize = @"5.7寸";
// NSLog(@"%@", phone->_phoneBrand);
// NSLog(@"%@", phone->_phoneColor);
// NSLog(@"%@", phone->_phoneModel);
// NSLog(@"%lg元", phone->_phonePrice);
// NSLog(@"%@", phone->_phoneSize);
//
// Student *stu = [[Student alloc] init];
// NSLog(@"%@", stu->_stuName);
//
// Student *stu1 = [[Student alloc] init];
// // self 就是自己
Cellphone *phone = [[Cellphonealloc]init];
NSLog(@"%g", phone->_phonePrice);
//
使用sayHi方法
[phonesayHi];
phone->_phoneModel =@"你好";
[phonesayHi];
return0;
}
//
// Cellphone.h
// OC01_类和对象
//
// Created by dllo on 15/7/15.
// Copyright (c) 2015年 Clare. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface Cellphone :NSObject
// 特征
{
@public//公共的,所以都能用
// @protected // 保护,当前类和子类能用
// @private // 私有,只有当前类能用
NSString *_phoneBrand;
NSString *_phoneModel;
NSString *_phoneColor;
NSString *_phoneSize;
CGFloat _phonePrice;
}
// 行为
- (void)call; //打电话
- (void)surfOnline;//上网
- (void)TFBoy; //贴膜
// 写一个用来打印全部信息的功能
- (void)sayHi;
@end
//
// Cellphone.m
// OC01_类和对象
//
// Created by dllo on 15/7/15.
// Copyright (c) 2015年 Clare. All rights reserved.
//
#import "Cellphone.h"
@implementation Cellphone
// 打电话
- (void)call{
NSLog(@"打电话");
}
// 上网
- (void)surfOnline{
NSLog(@"上网");
}
// 贴膜
- (void)TFBoy{
NSLog(@"贴膜");
}
// 重写电话的初始化方法
- (id)init
{
_phoneColor =
@"白色";
_phonePrice =
1999;
_phoneModel =
@"大屏";
_phoneBrand =
@"华为";
_phoneSize =
@"4.7寸";
return
self;
}
- (void)sayHi
{
NSLog(@"%@, %@, %@, %@, %g",_phoneBrand,_phoneModel,_phoneColor,_phoneSize,_phonePrice);
}
@end
//
// AudiCar.h
// OC01_类和对象
//
// Created by dllo on 15/7/15.
// Copyright (c) 2015年 Clare. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface AudiCar :NSObject
{
@public
NSString *_carName;
NSString *_carModel;
CGFloat _carPrice;
NSInteger _carTyre;
NSInteger *_carColor;
}
- (void)run;
- (void)refuel;
- (void)knock;
@end
//
// AudiCar.m
// OC01_类和对象
//
// Created by dllo on 15/7/15.
// Copyright (c) 2015年 Clare. All rights reserved.
//
#import "AudiCar.h"
@implementation AudiCar
- (void)run{
NSLog(@"飙车");
}
- (void)refuel{
NSLog(@"加油");
}
- (void)knock{
NSLog(@"撞车");
}
@end
//
// Student.h
// OC01_类和对象
//
// Created by dllo on 15/7/15.
// Copyright (c) 2015年 Clare. All rights reserved.
//
#import <Foundation/Foundation.h>
// @interface 接口文件,一个类的开始
// Student是当前的类名
// NSObject是继承的父类
// 类到@end才结束
@interface Student :NSObject
// 特征
{
@public
// @public成员变量的可见度
//成员变量,或实例变量
NSString *_stuName; //姓名
NSString *_stuSex; //性别
NSInteger _stuAge; //年龄
CGFloat _stuScore; //成绩
NSString *_stuHobby;//爱好
}
// 行为
// 打招呼
- (void)sayHi;
// 吃
- (void)eat;
// 玩
- (void)play;
@end
// 文件名和类名没有任何关系,但是为了方便对文件里的类进行管理,所以让文件名和类名相同
// 文件中可以写多个类,但是还是为了方便管理的原则,一个文件只写一个类
// 一个文件可以有多个类,但是为了方便管理,一个文件中只写一个类
//
// Student.m
// OC01_类和对象
//
// Created by dllo on 15/7/15.
// Copyright (c) 2015年 Clare. All rights reserved.
//
#import "Student.h"
// 对应的实现文件
@implementation Student
// 打招呼
- (void)sayHi{
NSLog(@"你好");
}
// 吃
- (void)eat{
NSLog(@"吃饭吧!");
}
// 玩
- (void)play{
NSLog(@"想玩");
}
// 重写继承过来的init方法
- (id)init //id == void *即无类型指针
{
_stuName =@"何岸";
_stuSex =@"女";
_stuHobby =
@"男";
_stuAge =20;
_stuScore =
100;
return
self;
}
@end
相关文章推荐
- iOS AvPlayer AvAudioPlayer音频的后台播放问题
- iOS学习第一天
- iOS学习笔记--(c基础2)
- iOS 录音并播放,使用AFN发送文件
- VT-x is disabled in the BIOS. (VERR_VMX_MSR_VMXON_DISABLED).
- iPhone 6/plus iOS Safari fieldset border 边框消失
- 【iOS学习】二、实现直播技术学习
- iOS_关联详解
- iOS 设置程序的图片 以及启动画面
- iOS开发系列--让你的应用“动”起来--动画
- iOS开发系列--并行开发其实很容易--多线程
- iOS开源项目 页面切换效果TransitionAnimation
- IOS 异或加密
- IOS代理
- 支持TableView上下拉刷新的库文件Demo
- iOS block的用法
- ios 中frame and bound
- iOS官方demo
- 仿照余额宝余额动态变动
- ios中图片选择及上传