oc程序设计 设计一个Car类,成员变量int cfosspeedd;在设计一个方法comparecfosspeedd

基于SNMP的校园网络管理系统设计与实现_百度文库
两大类热门资源免费畅读
续费一年阅读会员,立省24元!
评价文档:
&&¥1.00
&&¥2.00
&&¥2.00
&&¥2.00
&&¥1.00
&&¥3.00
喜欢此文档的还喜欢
基于SNMP的校园网络管理系统设计与实现
阅读已结束,如果下载本文需要使用
想免费下载本文?
把文档贴到Blog、BBS或个人站等:
普通尺寸(450*500pix)
较大尺寸(630*500pix)
你可能喜欢OC_语法入门_day1_类的定义 - 推酷
OC_语法入门_day1_类的定义
H:/Objective-C/OC_day0/00-OC经典入门.m
/*================第一段代码(start)========================== =
#import &Foundation/Foundation.h&
// 1,类的声明
@interface Car : NSObject
// 所有成员变量,默认初始值均为0
// 对象的方法
// 1,前面固定写减号-
// 2,不同于函数,小括号()仅用来括住数据类型
// 3,参数和返回值,都必须用小括号包起来(void)
// 2,类的实现,专门用来实现成员方法
@implementation Car
// - (void)把声明复制过来,结尾的分号换成一对大括号
- (void)run
NSLog(@&I am driving~&);
// 成员方法内直接访问成员变量
NSLog(@&I have %d个wheels~&,wheels);
int main(){
// 3,执行Car类的new行为,返回new出来的对象地址
Car *p = [Car new];
// 4,类似结构体访问,给成员赋值
p-&wheels = 4;
// 5,打印其成员值
NSLog(@&车轮个数是:%d&,p-&wheels);
// 6,执行行为,OC中对象仅能通过指针访问,
// 给指针p所指向的对象,发送一条run消息,消息机制
================第一段代码(end)========================== = */
/*================第2段代码(start)========================== =
#import &Foundation/Foundation.h&
// 定义枚举(类型前要有空格),类型为性别
typedef enum{
SexBoy,SexGirl,SexUnknow
// 定义枚举(类型前要有空格),类型为颜色
typedef enum{
ColorRed,ColorGreen,ColorBlue
// 定义结构体(类型前要有空格),类型为日期时间型
typedef struct{
// 定义字符串类型,仿Java
typedef char *S
// 1,喵类的声明
@interface Cat : NSObject
// 所有成员变量,默认初始值均为0
Color furC
// 对象的方法
// 1,前面固定写减号-
// 2,不同于函数,小括号()仅用来括住数据类型
// 3,参数和返回值,都必须用小括号包起来(void)
// 2,类的实现,专门用来实现成员方法
@implementation Cat
// - (void)把声明复制过来,结尾的分号换成一对大括号
- (void)eat
// 成员方法内直接访问成员变量
NSLog(@&喵名:%s,毛色:%d,喵正在吃东西~&,name,furColor);
// 1,人类的声明
@interface Person : NSObject
// 所有成员变量,默认初始值均为0
Color favouriteC
// 对象的成员变量还是对象,OC任何对象都是指针指过去的~
// 对象的方法
// 1,前面固定写减号-
// 2,不同于函数,小括号()仅用来括住数据类型
// 3,参数和返回值,都必须用小括号包起来(void)
- (void)toS
- (void)feedC
// 4,带0个参数的方法声明
- (double)getPI;
// 5,带1个参数的方法声明,方法名为square:
- (int)square:(int)a;
// 6,带2个参数的方法声明,方法名为sumWithA:andB:
- (int)sumWithA:(int)a andB:(int)b;
// 7,参数为指向另一个对象的指针
- (int)compareAgeWithOtherPerson:(Person *)
// 2,类的实现,专门用来实现成员方法
@implementation Person
// - (void)toS把声明复制过来,结尾的分号换成一对大括号
- (void)toString
// 成员方法内直接访问成员变量
NSLog(@&姓名:%s,性别:%d,年龄:%d,生日:%d-%d-%d,喜欢的颜色:%d~&,name,sex,age,birthday.year,birthday.month,birthday.day,favouriteColor);
- (void)feedCat
// 间接执行成员变量cat的eat行为,完成喂喵操作~
[cat eat];
// 4,带0个参数的方法实现
- (double)getPI
NSLog(@&%.5f&,3.14159);
return 3.14159;
// 5,带1个参数的方法实现
- (int)square:(int)a
NSLog(@&%d&,a*a);
return a*a;
// 6,带2个参数的方法实现
- (int)sumWithA:(int)a andB:(int)b
NSLog(@&%i&,a+b);
return a+b;
// 7,参数为指向另一个对象的指针
- (int)compareAgeWithOtherPerson:(Person *)other
NSLog(@&年龄差距为:%i&,age-other-&age);
return age-other-&
int main(){
// 3,执行Car类的new行为,返回new出来的对象地址
Person *p = [Person new];
// 4,类似结构体访问,给成员赋值
p-&name = &pre_eminent&;
p-&sex = SexB
p-&age = 24;
// 下面是错误的,结构体大括号这种赋值,只有在定义变量的时候才可以,类似的还有数组
// p-&birthday = {};
Date d = {};
// 同基本数据类型一样,结构体间可整体赋值(传值)
p-&birthday =
// 指针访问结构体成员用-&,结构体变量访问其成员用.
p-&birthday.month = 7;
p-&birthday.day = 6;
p-&favouriteColor = ColorB
// 6,执行行为,OC中对象仅能通过指针访问,
// 给指针p所指向的对象,发送一条toString消息,消息机制
[p toString];
// 7,为成员变量cat赋值
Cat *c = [Cat new];
c-&name = &Cafee&;
c-&furColor = ColorG
// 将8个字节长的地址给了成员变量cat指针
[p feedCat];
// 9,执行方法示例,参数和参数之前要有空格,1个参数对应1个冒号
[p getPI];
[p square:16];
[p sumWithA:6 andB:7];
Person *other = [Person new];
other-&age = 19;
[p compareAgeWithOtherPerson:other];
================第2段代码(end)========================== = */
/*============第3段代码start=======================
#import &Foundation/Foundation.h&
// 定义枚举(类型前要有空格),类型为性别
typedef enum{
SexBoy,SexGirl,SexUnknow
// 定义枚举(类型前要有空格),类型为颜色
typedef enum{
ColorRed,ColorGreen,ColorBlue
// 定义结构体(类型前要有空格),类型为日期时间型
typedef struct{
// 定义字符串类型,仿Java
typedef char *S
// 1,喵类的声明
@interface Cat : NSObject
// 所有成员变量,默认初始值均为0
// @public 以后所有成员变量均只通过set和get方法访问,且成员变量以_开头
Color _furC
// 类的方法注意事项:
// 2,只能类调用,不能对象调用,因为+_号不同
// 3,允许类方法与对象方法重名,因为还有+_号可以区分它俩
// 4,同Java一样,类方法无法访问对象成员变量,因为类加载的时候,对象还没创建,而成员变量仅存在对象内存空间
// 注:类内存空间只保存方法列表,即类的,对象的~
+ (void)showClassN
// 对象的方法
// 1,前面固定写减号-
// 2,不同于函数,小括号()仅用来括住数据类型
// 3,参数和返回值,都必须用小括号包起来(void)
// 4,_name的set方法
- (void)setName:(String)
// 5,_furColor的set方法
- (void)setFurColor:(Color)furC
// 6,_name的get方法,同Java不同,get方法名就是成员变量名
- (String)
// 7,_furColor的get方法,同Java不同,get方法名就是成员变量名
- (Color)furC
// 2,类的实现,专门用来实现成员方法
@implementation Cat
// 类的方法实现
+ (void)showClassName
NSLog(@&我是喵类&);
// - (void)把声明复制过来,结尾的分号换成一对大括号
- (void)eat
// 成员方法内直接访问成员变量
NSLog(@&喵名:%s,毛色:%d,喵正在吃东西~&,_name,_furColor);
// _name的set方法
- (void)setName:(String)name
// _furColor的set方法
- (void)setFurColor:(Color)furColor
_furColor = furC
// _name的get方法
- (String)name
// _furColor的get方法
- (Color)furColor
return _furC
int main(){
// 类的方法注意事项:
// 2,只能类调用,不能对象调用,因为+_号不同
// 3,允许类方法与对象方法重名,因为还有+_号可以区分它俩
// 4,同Java一样,类方法无法访问对象成员变量,因为类加载的时候,对象还没创建,而成员变量仅存在对象内存空间
// 注:类内存空间只保存方法列表,即类的,对象的~
[Cat showClassName];
Cat *c = [Cat new];
[c setName:&Cafee&];
[c setFurColor:ColorGreen];
NSLog(@&喵大名是:%s---颜色是:%d&,[c name],[c furColor]);
// 1,创建OC字符串方式一
NSString *url = @&http:// &;
char *email = &admin@g.fr&;
NSLog(@&url is %@,email is %s&,url,email);
// 2,创建OC字符串方式二,类方法
NSString *info = [NSString stringWithFormat:@&---url is %@,email is %s&,url,email];
NSLog(@&先写占位符号:%@&,info);
// 3,计算长度,OC中算字数,C语言算字节数
NSString *str = @&你好中国&;
NSLog(@&%@的长度是:%ld&,str,[str length]);
//NSArray *arr=@[@&beyond1&,@&beyond2&,@&beyond3&];
NSArray *arr=[NSArray arrayWithObject:@&beyond&];
//NSLog(@&%@&,[arr ObjectAtIndex:0]);
============第3段代码ends===========*/
#import &Foundation/Foundation.h&
// 遍历文件夹下面所有的文件,含子文件夹,输出里面所有的.c文件内容
path : 文件的全路径(可能是文件夹、也可能是文件)
返回值 void
void listFiles(NSString *dir)
// 1.获得文件管理者,单例
NSFileManager *mgr = [NSFileManager defaultManager];
// 2.标记是否为文件夹
BOOL isDir = YES; // 标记是否为文件夹
// 标记这个路径是否存在
BOOL exist = [mgr fileExistsAtPath:dir isDirectory:&isDir];
// 3.如果不存在,直接返回0
if(!exist){
NSLog(@&文件路径不存在!!!!!!%@&,dir);
// 代码能来到着,说明路径存在
if (isDir){ // 文件夹
// 获得当前文件夹path下面的所有内容(文件夹、文件)
NSArray *array = [mgr contentsOfDirectoryAtPath:dir error:nil];
// 遍历数组中的所有子文件(夹)名
for (NSString *filename in array){
// 获得子文件(夹)的全路径
NSString *fullPath = [NSString stringWithFormat:@&%@/%@&, dir, filename];
//关键,继续递归
listFiles(fullPath);
}else{ // 文件
// 判断文件的拓展名(忽略大小写)
NSString *ext = [[dir pathExtension] lowercaseString];
if (![ext isEqualToString:@&h&]
&& ![ext isEqualToString:@&m&]
&& ![ext isEqualToString:@&c&])
// 文件拓展名不是h,而且也不是m,而且也不是c
// 加载文件内容
NSString *content = [NSString stringWithContentsOfFile:dir encoding:NSUTF8StringEncoding error:nil];
// 将文件内容切割为每一行
//NSArray *array = [content componentsSeparatedByString:@&\n&];
// 输出文件内容
NSLog(@&%@&,content);
int main(){
NSString *dir=@&H:/PPT03-Objective-C/OC代码&;
listFiles(dir);
H:/Objective-C/OC_day1/01-第1个OC程序.m
#include &stdio.h&
// OC程序的入口:main函数
int main()
printf(&第1个OC程序\n&);
H:/Objective-C/OC_day1/02-第2个OC程序.m
1.#import的用途:
1& 跟#include一样,拷贝文件的内容
2& 可以自动防止文件的内容被重复拷贝
2.#import &Foundation/NSObjCRuntime.h&
NSObjCRuntime.h中有NSLog函数的声明
3.Foundation框架头文件的路径
1& 右击Xcode.app --& 显示包内容
2& Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS6.0.sdk/System/Library/Frameworks/Foundation.framework
4.主头文件
1& 主头文件:最主要的头文件,名字一般跟框架名称一样,包含了框架中的所有其他头文件
2& Foundation框架的主头文件名称就是Foundation.h
3& 只需要包含Foundation框架主头文件,就可以使用整个框架的东西
5.运行过程
1& 编写OC源文件:.m、.c
2& 编译:cc -c xxx.m xxx.c
3& 链接:cc xxx.o xxx.o -framework Foundation
(只有用到了Foundation框架才需要加上-framework Foundation)
4& 运行:./a.out
#import &Foundation/Foundation.h&
int main()
// NSLog输出内容会自动换行
NSLog(@&第2个OC程序!!!&);
H:/Objective-C/OC_day1/05-BOOL类型.m
#import &Foundation/Foundation.h&
BOOL test(BOOL mybool)
return NO;
int main()
BOOL b = YES;
BOOL b2 = NO;
BOOL b3 = 1; // YES
BOOL b4 = 0; // NO
//NSLog(@&%i&, b);
NSLog(@&%d&, test(YES));
H:/Objective-C/OC_day1/06-类的设计_头脑风暴.m
1.类的设计:
* 类名的第一个字母必须是大写
* 不能有下划线
* 多个英文单词,用驼峰标识
3& 行为(功能)
2.植物大战僵尸的僵尸
* 类名:Zoombie
* 属性:life、speed、gongjili
* 行为:walk、bite、die
3.雷电的飞机
* 类名:Plane
* 属性:life、gongjili、speed、bombCount
* 行为:fly、bomb、shoot、die
* 类名:Computer
* 属性:band、expireDate
* 行为:open、close
H:/Objective-C/OC_day1/07-第1个OC的类.m
属性:轮胎个数、时速(速度)
// 因为使用了NSObject
#import &Foundation/Foundation.h&
// 完整地写一个函数:函数的声明和定义(实现)
// 完整地写一个类:类的声明和实现
// 1.类的声明
// 声明对象的属性、行为
// : NSObject 目的是:让Car这个类具备创建对象的能力
@interface Car : NSObject
{// 用来声明对象属性(实例变量\成员变量,默认会初始化为0)
// @public可以让外部的指针间接访问对象内部的成员变量
// 轮胎个数
// 时速(xxkm/h)
// 方法(行为):方法名、参数、返回值(声明、实现)
// 只要是OC对象的方法,必须以减号 - 开头
// OC方法中任何数据类型都必须用小括号()扩住
// OC方法中的小括号():括住数据类型
// 2.类的实现
// 用来实现@inteface中声明的方法
@implementation Car
// 方法的实现(说清楚方法里面有什么代码)
- (void)run
NSLog(@&车子跑起来了&);
int main()
// 在OC中,想执行一些行为,就写上一个中括号[行为执行者 行为名称]
// 利用类来创建对象
// 执行了Car这个类的new行为来创建新对象
// 定义了一个指针变量p,p将来指向的是Car类型的对象
// [Car new]每次都会创建出一个新对象,并且会返回新对象本身(新对象的地址)
Car *p = [Car new];
Car *p2 = [Car new];
p2-&wheels = 5;
p2-&speed = 300;
// 给p所指向对象的wheels属性赋值
p-&wheels = 4;
p-&speed = 250;
// 给p所指向对象发送一条run消息
NSLog(@&车子有%d个轮子,时速位:%dkm/h&, p-&wheels, p2-&speed);
H:/Objective-C/OC_day1/08-第2个OC的类.m
类名:Person
属性(成员变量\实例变量):体重、年龄
行为(方法):走路、吃
#import &Foundation/Foundation.h&
1.类的声明
* 成员变量
* 方法的声明
@interface Person : NSObject
// 2.类的实现
@implementation Person
// 实现@interface中声明的方法
- (void)walk
NSLog(@&%d岁、%f公斤的人走了一段路&, age, weight);
- (void)eat
NSLog(@&%d岁、%f公斤的人在吃东西&, age, weight);
int main()
// 在使用类创建对象之前,会将类加载进内存
Person *p = [Person new];
p-&age = 20;
p-&weight = 40;
Person *p2 = [Person new];
p2-&age = 30;
p2-&weight = 60;
[p2 walk];
Person *p2 = [Person new];
p2-&age = 30;
p2-&weight = 50;
p-&age = 40;
[p2 walk];
Person *p = [Person new];
p-&age = 20;
Person *p2 = [Person new];
p2-&weight = 50.0;
Person *p = [Person new];
p-&age = 20;
p-&weight = 50.0;
Person *p2 = [Person new];
p2-&age = 30;
p2-&weight = 60.0;
[p2 walk];
H:/Objective-C/OC_day1/09-OC对象跟函数.m
#import &Foundation/Foundation.h&
@interface Car : NSObject
{// 成员变量
@implementation Car
- (void)run
NSLog(@&%d个轮子,速度为%dkm/h的车子跑起来&, wheels, speed);
void test(int w, int s)
void test1(Car *newC)
newC-&wheels = 5;
void test2(Car *newC)
Car *c2 = [Car new];
c2-&wheels = 5;
c2-&speed = 300;
newC = c2;
newC-&wheels = 6;
int main()
Car *c = [Car new];
c-&wheels = 4;
c-&speed = 250;
//test(c-&wheels, c-&speed);
//test1(c);
H:/Objective-C/OC_day1/10-方法和函数_常见错误.m
1.对象方法都是以减号 -
2.对象方法的声明必须写在@interface和@end之间
对象方法的实现必须写在@implementation和@end之间
3.对象方法只能由对象来调用
4.对象方法归类\对象所有
1.函数能写在文件中的任意位置(@interface和@end之间除外),函数归文件所有
2.函数调用不依赖于对象
3.函数内部不能直接通过成员变量名访问某个对象的成员变量
#import &Foundation/Foundation.h&
@interface Person : NSObject
@implementation Person
@interface Car : NSObject
{// 成员变量\实例变量
//int wheels = 4; 不允许在这里初始化
// 不能随便将成员变量当做C语言中的变量来使用
int main()
// wheels = 10;
Car *c = [Car new];
c-&wheels = 4;
void test2();
@implementation Car
- (void) fly
void test2()
NSLog(@&调用了test2函数-%d&, wheels);
void test()
NSLog(@&调用了test函数&);
- (void)run
NSLog(@&%d个轮子的车跑起来了&, wheels);
H:/Objective-C/OC_day1/11-类的合理设计.m
#import &Foundation/Foundation.h&
typedef enum {
typedef struct {
typedef enum {
ColorBlack,
ColorGreen
@interface Dog : NSObject
Color curC // 毛色
@implementation Dog
- (void)eat
// 每吃一次,体重就加1
weight += 1;
//weight = weight +
NSLog(@&狗吃完这次后的体重是%f&, weight);
- (void)run
weight -= 1;
NSLog(@&狗跑完这次后的体重是%f&, weight);
成员变量:性别、生日、体重、最喜欢的颜色、狗(体重、毛色,吃、跑)
方法:吃、跑步、遛狗(让狗跑)、喂狗(让狗吃)
@interface Student : NSObject
// 体重(kg)
Color favC // 最喜欢的颜色
// 重点:狗
- (void)liuD
- (void)weiD
@implementation Student
- (void)liuDog
// 让狗跑起来(调用狗的run方法)
[dog run];
- (void)weiDog
// 让狗吃东西(调用狗的eat方法)
[dog eat];
- (void)print
NSLog(@&性别=%d, 喜欢的颜色=%d, 姓名=%s, 生日=%d-%d-%d&, sex, favColor, name, birthday.year, birthday.month, birthday.day);
- (void)eat
// 每吃一次,体重就加1
weight += 1;
//weight = weight +
NSLog(@&学生吃完这次后的体重是%f&, weight);
- (void)run
weight -= 1;
NSLog(@&学生跑完这次后的体重是%f&, weight);
int main()
Student *s = [Student new];
Dog *d = [Dog new];
d-&curColor = ColorG
d-&weight = 20;
[s liuDog];
[s weiDog];
void test()
Student *s = [Student new];
s-&weight = 50;
s-&sex = SexM
Date d = {};
s-&birthday =
s-&name = &Jack&;
s-&birthday.year = 2011;
s-&birthday.month = 9;
s-&birthday.day = 10;
// 喜欢的颜色
s-&favColor = ColorB
[s print];
H:/Objective-C/OC_day1/12-方法的声明和实现.m
#import &Foundation/Foundation.h&
1& 返回 π
2& 计算某个整数的平方
3& 计算两个整数的和
@interface JiSuanQi : NSObject
// 方法名:pi
- (double)
// OC方法中,一个参数对应一个冒号
// 方法名:pingFang:(冒号也是方法名的一部分)
- (int)pingFang:(int)
//- (int)sum:(int)num1 :(int)num2;
// 方法名:sumWithNum1:andNum2:
- (int)sumWithNum1:(int)num1 andNum2:(int)num2;
//- (int)sumWithNum1:(int)num1 andNum2:(int)num2 andNum3:(int)num3;
@implementation JiSuanQi
- (double)pi
return 3.14;
- (int)pingFang:(int)num
return num *
//- (int)sum:(int)num1 :(int)num2
- (int)sumWithNum1:(int)num1 andNum2:(int)num2
return num1 + num2;
int main()
JiSuanQi *jsq = [JiSuanQi new];
int a = [jsq sumWithNum1:20 andNum2:5];
//int a = [jsq sum:10 :5];
[jsq pingFang:10];
//double a = [jsq pi];
NSLog(@&%i&, a);
H:/Objective-C/OC_day1/13-练习.m
设计一个方法,用来和其他车比较车速,返回车速的差距
#import &Foundation/Foundation.h&
@interface Car : NSObject
- (int)compareSpeedWithOther:(Car *)
@implementation Car
- (int)compareSpeedWithOther:(Car *)other
// other-&speed
// 返回当前这辆车和other这辆车的速度差距
return speed - other-&
int main()
Car *c1 = [Car new];
c1-&speed = 300;
Car *c2 = [Car new];
c2-&speed = 250;
int a = [c1 compareSpeedWithOther:c2];
NSLog(@&a=%d&, a);
H:/Objective-C/OC_day1/14-匿名对象.m
#import &Foundation/Foundation.h&
@interface Car : NSObject
@implementation Car
- (void)run
NSLog(@&速度为%d的车子跑起来了&, speed);
int main()
c = [Car new];
c-&speed = 250;
[c run];*/
// 不要写类似匿名对象这样的代码
// 只要求能说出输出结果
[Car new]-&speed = 300;
[[Car new] run];
//Car *c = [Car new];
H:/Objective-C/OC_day1/15-01-改错题-答案.m
第1题:类的声明和定义、对象的创建
//#import &Foundation.h&
前面要加上框架名称&Foundation/&
#import &Foundation/Foundation.h&
@interface Person : NSObject
// @public能让外面的指针访问对象内部的成员变量
// 成员变量不能在类声明中进行初始化
//double height = 1.55;
// 方法的声明不能写在{}中
// - (void)
// 每个@interface都必须和一个@end配对
@implementation Person
- (void)study
NSLog(@&年龄为%d的人在学习&, age);
int main()
// 要利用指针变量来指向对象
//Person p = [Person new];
Person *p = [Person new];
p-&age = 10;
p-&height = 1.78f;
[p study];
// 5处错误
第2题:方法的声明和实现
#import &Foundation/Foundation.h&
@interface Test : NSObject
// 一个参数对应一个冒号
//- (int)addNum1(int)num1 andNum2(int)num2;
- (int)addNum1:(int)num1 andNum2:(int)num2;
// 没有参数就不需要冒号
//- (double)pi:;
- (double)
// 小括号()在方法中只用来括住具体类型
//- (void)test();
@implementation Test
//- (int)addNum1(int)num1 andNum2(int)num2()
- (int)addNum1:(int)num1 andNum2:(int)num2
return num1 + num2;
//- (double)pi:
- (double)pi
return 3.14;
//- (void)test()
- (void)test
int main()
// 6处错误
第3题:方法和函数的区别
#import &Foundation/Foundation.h&
@interface Car : NSObject
@implementation Car
- (void)test
NSLog(@&测试一下车子:%i&, wheels);
// OC方法的实现跟函数是不一样的
// void run()
- (void)run
NSLog(@&%i个轮子的车跑起来了&, wheels);
// 方法不能独立于类存在,类外面只能定义函数
//- (void)haha
void haha()
NSLog(@&调用了haha&);
int main()
Car *c = [Car new];
// OC方法不能当做函数来调用,需要用对象来调用
// test();
// 3处错误
H:/Objective-C/OC_day1/15-02-分析题-答案-.m
第1题:对象的操作、对象作为函数参数
#import &Foundation/Foundation.h&
@interface Person : NSObject
void test1(int newAge, double newHeight);
void test2(Person *newP);
void test3(Person *newP);
void test4(Person *newP);
int main()
Person *p = [Person new];
p-&age = 10;
p-&height = 1.5f;
test1(p-&age, p-&height);
[p print];
[p print];
[p print];
[p print];
@implementation Person
- (void)print
NSLog(@&年龄=%d,身高=%f&, age, height);
void test1(int newAge, double newHeight)
newAge = 10;
newHeight = 1.6;
void test2(Person *newP)
newP-&age = 20;
newP-&height = 1.7;
void test3(Person *newP)
Person *p2 = [Person new];
p2-&age = 40;
p2-&height = 1.8;
newP = p2;
newP-&age = 30;
void test4(Person *newP)
Person *p2 = newP;
p2-&age = 50;
p2-&height = 1.9;
newP-&age = 60;
====================================================
第1题:对象的操作、对象作为函数参数
年龄=10,身高=1.500000
年龄=20,身高=1.700000
年龄=20,身高=1.700000
年龄=60,身高=1.900000
====================================================
第2题:匿名对象
#import &Foundation/Foundation.h&
@interface Person : NSObject
@implementation Person
- (void)print
NSLog(@&年龄=%d,身高=%f&, age, height);
int main()
[Person new]-&age = 10;
[Person new]-&height = 1.8;
[[Person new] print];
=======================================
第2题:匿名对象
年龄=0,身高=0.000000
=======================================
H:/Objective-C/OC_day1/15-03-类的设计-Dog.m
设计一个”狗“类
* 速度(单位是m/s)
* 体重(单位是kg)
* 吃:每吃一次,体重增加0.5kg,输出吃完后的体重
* 吠(叫):输出所有的属性
* 跑:每跑一次,体重减少0.5kg,输出速度和跑完后的体重
* 比较颜色:跟别的狗比较颜色,如果一样,返回YES,不一样,返回NO
* 比较速度:跟别的狗比较速度,返回速度差(自己的速度 - 其他狗的速度)
#import &Foundation/Foundation.h&
typedef enum {
ColorBlack,
ColorGreen,
ColorBlue,
typedef enum {
@interface Dog : NSObject
- (void) // 吃
- (void) // 吠
- (void) // 跑
- (BOOL)isSameColorWithOther:(Dog *) // 比较是否为相同颜色
- (int)compareSpeedWithOther:(Dog *) // 比较速度差
@implementation Dog
- (void)eat // 吃
weight += 0.5;
NSLog(@&狗吃完后的体重是%f公斤&, weight);
- (void)bark // 吠
NSLog(@&狗的颜色是%d,速度为%dkm/h,性别是%d,体重是%f公斤&, color, speed, sex, weight);
- (void)run // 跑
weight -= 0.5;
NSLog(@&狗跑完后的体重是%f公斤&, weight);
- (BOOL)isSameColorWithOther:(Dog *)other // 比较是否为相同颜色
return color == other-&
- (int)compareSpeedWithOther:(Dog *)other // 比较速度差
return speed - other-&
H:/Objective-C/OC_day1/15-04-类的设计-Person.m
2.结合前面的“狗”类,设计一个“人”类
* 狗(养了一条狗)
* 喂狗:每喂一次,狗就会执行“吃”这个行为
* 遛狗:每溜一次,狗就会执行“跑”这个行为
#import &Foundation/Foundation.h&
typedef enum {
ColorBlack,
ColorGreen,
ColorBlue,
typedef enum {
/*********************狗********************/
@interface Dog : NSObject
- (void) // 吃
- (void) // 吠
- (void) // 跑
- (BOOL)isSameColorWithOther:(Dog *) // 比较是否为相同颜色
- (int)compareSpeedWithOther:(Dog *) // 比较速度差
@implementation Dog
- (void)eat // 吃
weight += 0.5;
NSLog(@&狗吃完后的体重是%f公斤&, weight);
- (void)bark // 吠
NSLog(@&狗的颜色是%d,速度为%dkm/h,性别是%d,体重是%f公斤&, color, speed, sex, weight);
- (void)run // 跑
weight -= 0.5;
NSLog(@&狗跑完后的体重是%f公斤&, weight);
- (BOOL)isSameColorWithOther:(Dog *)other // 比较是否为相同颜色
return color == other-&
- (int)compareSpeedWithOther:(Dog *)other // 比较速度差
return speed - other-&
/*********************人********************/
@interface Person : NSObject
char * // 姓名
Dog * // 狗
- (void)walkD // 遛狗
- (void)feedD // 喂狗
@implementation Person
- (void)walkDog // 遛狗
[dog run];
- (void)feedDog // 喂狗
[dog eat];
H:/Objective-C/OC_day1/15-05-类的设计-Studeng类1.m
3.设计一个”学生“类
* 身高(单位是m)
* 体重(单位是kg)
* C语言成绩
* 跑步:每跑步一次,身高增加1cm,体重减小0.5kg,输出跑完后的体重
* 吃饭:每吃一次,身高增加1cm,体重增加0.5kg,输出吃完后的体重
* 学习:每学习一次,3可成绩各加1分,输出学习完后的3科成绩
* 睡觉:输出所有的属性
* 比较C语言成绩:跟另外一个学生比较C语言成绩,返回成绩差(自己的成绩 - 其他人的成绩)
* 比较OC成绩:跟另外一个学生比较OC语言成绩,返回成绩差(自己的成绩 - 其他人的成绩)
* 比较iOS成绩:跟另外一个学生比较iOS语言成绩,返回成绩差(自己的成绩 - 其他人的成绩)
* 计算总分:算出3科成绩的总分
* 计算平均分:算出3科成绩的平均分
#import &Foundation/Foundation.h&
typedef struct {
typedef enum {
@interface Student : NSObject
char * // 姓名
int cS // C语言成绩
int ocS // OC成绩
int iosS // iOS成绩
- (void) // 跑
- (void) // 吃
- (void) // 学习
- (void) // 睡觉
- (int)compareCScoreWithOther:(Student *) // 比较C成绩
- (int)compareOcScoreWithOther:(Student *) // 比较OC成绩
- (int)compareIosScoreWithOther:(Student *) // 比较iOS成绩
- (int)totalS // 总分
- (int)averageS // 平均分
@implementation Student
- (void)run // 跑
height += 0.01;
weight -= 0.5;
NSLog(@&学生跑完后的体重是%f公斤&, weight);
- (void)eat // 吃
height += 0.01;
weight += 0.5;
NSLog(@&学生吃完后的体重是%f公斤&, weight);
- (void)study // 学习
cScore += 1;
ocScore += 1;
iosScore += 1;
- (void)sleep // 睡觉
NSLog(@&姓名=%s,身高=%f米,体重=%f公斤,生日=%d-%d-%d,年龄=%d岁,性别=%d,C成绩=%d,OC成绩=%d,iOS成绩=%d&, name, height, weight, birthday.year, birthday.month, birthday.day, age, sex,
cScore, ocScore, iosScore);
- (int)compareCScoreWithOther:(Student *)other // 比较C成绩
return cScore - other-&cS
- (int)compareOcScoreWithOther:(Student *)other // 比较OC成绩
return ocScore - other-&ocS
- (int)compareIosScoreWithOther:(Student *)other // 比较iOS成绩
return iosScore - other-&iosS
- (int)totalScore // 总分
return cScore + ocScore + iosS
- (int)averageScore // 平均分
return (cScore + ocScore + iosScore) / 3;
H:/Objective-C/OC_day1/15-06-类的设计-成绩类.m
4.设计一个成绩类
* C语言成绩
* 比较C语言成绩:跟另外一个成绩对象比较C语言成绩,返回成绩差(自己 - 其他成绩)
* 比较OC成绩:跟另外一个成绩对象比较OC语言成绩,返回成绩差(自己 - 其他成绩)
* 比较iOS成绩:跟另外一个成绩对象比较iOS语言成绩,返回成绩差(自己 - 其他成绩)
* 计算总分:算出3科成绩的总分
* 计算平均分:算出3科成绩的平均分
#import &Foundation/Foundation.h&
@interface Score : NSObject
int cS // C语言成绩
int ocS // OC成绩
int iosS // iOS成绩
- (int)compareCScoreWithOther:(Score *) // 比较C成绩
- (int)compareOcScoreWithOther:(Score *) // 比较OC成绩
- (int)compareIosScoreWithOther:(Score *) // 比较iOS成绩
- (int)totalS // 总分
- (int)averageS // 平均分
@implementation Score
- (int)compareCScoreWithOther:(Score *)other // 比较C成绩
return cScore - other-&cS
- (int)compareOcScoreWithOther:(Score *)other // 比较OC成绩
return ocScore - other-&ocS
- (int)compareIosScoreWithOther:(Score *)other // 比较iOS成绩
return iosScore - other-&iosS
- (int)totalScore // 总分
return cScore + ocScore + iosS
- (int)averageScore // 平均分
return (cScore + ocScore + iosScore) / 3;
H:/Objective-C/OC_day1/15-07-类的设计-Student类2.m
5.利用前面的成绩类,重新设计一个学生类
* 成绩(包括3科成绩)
* 比较C语言成绩:跟另外一个学生比较C语言成绩,返回成绩差(自己的成绩 - 其他人的成绩)
* 比较OC成绩:跟另外一个学生比较OC语言成绩,返回成绩差(自己的成绩 - 其他人的成绩)
* 比较iOS成绩:跟另外一个学生比较iOS语言成绩,返回成绩差(自己的成绩 - 其他人的成绩)
* 比较总分:跟另外一个学生比较总分,返回成绩差(自己的成绩 - 其他人的成绩)
* 比较平均分:跟另外一个学生比较平均分,返回成绩差(自己的成绩 - 其他人的成绩)
#import &Foundation/Foundation.h&
/****************成绩******************/
@interface Score : NSObject
int cS // C语言成绩
int ocS // OC成绩
int iosS // iOS成绩
- (int)compareCScoreWithOther:(Score *) // 比较C成绩
- (int)compareOcScoreWithOther:(Score *) // 比较OC成绩
- (int)compareIosScoreWithOther:(Score *) // 比较iOS成绩
- (int)totalS // 总分
- (int)averageS // 平均分
@implementation Score
- (int)compareCScoreWithOther:(Score *)other // 比较C成绩
return cScore - other-&cS
- (int)compareOcScoreWithOther:(Score *)other // 比较OC成绩
return ocScore - other-&ocS
- (int)compareIosScoreWithOther:(Score *)other // 比较iOS成绩
return iosScore - other-&iosS
- (int)totalScore // 总分
return cScore + ocScore + iosS
- (int)averageScore // 平均分
return (cScore + ocScore + iosScore) / 3;
/********************学生********************/
@interface Student : NSObject
char * // 姓名
Score * // 成绩
- (int)compareCScoreWithOther:(Student *) // 比较C成绩
- (int)compareOcScoreWithOther:(Student *) // 比较OC成绩
- (int)compareIosScoreWithOther:(Student *) // 比较iOS成绩
- (int)compareTotalScoreWithOther:(Student *) // 比较总成绩
- (int)compareAverageScoreWithOther:(Student *) // 比较平均成绩
@implementation Student
- (int)compareCScoreWithOther:(Student *)other // 比较C成绩
return [score compareCScoreWithOther:other-&score];
- (int)compareOcScoreWithOther:(Student *)other // 比较OC成绩
return [score compareOcScoreWithOther:other-&score];
- (int)compareIosScoreWithOther:(Student *)other // 比较iOS成绩
return [score compareIosScoreWithOther:other-&score];
- (int)compareTotalScoreWithOther:(Student *)other // 比较总成绩
return [score totalScore] - [other-&score totalScore];
- (int)compareAverageScoreWithOther:(Student *)other // 比较平均成绩
return [score averageScore] - [other-&score averageScore];
已发表评论数()
&&登&&&陆&&
已收藏到推刊!
请填写推刊名
描述不能大于100个字符!
权限设置: 公开
仅自己可见

我要回帖

更多关于 oc成员变量 的文章

 

随机推荐