OC_语法入门_day1_类的定义

H:/Objective-C/OC_day0/00-OC经典入门.m
================第一段代码(start)========================== = 
#import <Foundation/Foundation.h>
// 1,类的声明
	@interface Car : NSObject
	{	
		// 所有成员变量,默认初始值均为0
		@public
		int wheels;
		int speed;
	}
	// 对象的方法
	// 1,前面固定写减号-
	// 2,不同于函数,小括号()仅用来括住数据类型
	// 3,参数和返回值,都必须用小括号包起来(void)
	- (void)run;
	@end
// 2,类的实现,专门用来实现成员方法
	@implementation Car
	// - (void)run;把声明复制过来,结尾的分号换成一对大括号
	- (void)run
	{
		NSLog(@"I am driving~");
		// 成员方法内直接访问成员变量
		NSLog(@"I have %d个wheels~",wheels);
	}
	@end
	
int main(){
	// 3,执行Car类的new行为,返回new出来的对象地址
	Car *p = [Car new];
	// 4,类似结构体访问,给成员赋值
	p->wheels = 4;
	// 5,打印其成员值
	NSLog(@"车轮个数是:%d",p->wheels);
	// 6,执行行为,OC中对象仅能通过指针访问,
	// 给指针p所指向的对象,发送一条run消息,消息机制
	[p run];
	return 0;
}
================第一段代码(end)========================== = 
================第2段代码(start)========================== = 
#import <Foundation/Foundation.h>
// 定义枚举(类型前要有空格),类型为性别
typedef enum{
	SexBoy,SexGirl,SexUnknow
} Sex;
// 定义枚举(类型前要有空格),类型为颜色
typedef enum{
	ColorRed,ColorGreen,ColorBlue
} Color;
// 定义结构体(类型前要有空格),类型为日期时间型
typedef struct{
	int year;
	int month;
	int day;
} Date;
// 定义字符串类型,仿Java
typedef char *String;
// 1,喵类的声明
	@interface Cat : NSObject
	{	
		// 所有成员变量,默认初始值均为0
		@public
		String name;		
		Color furColor;
	}
	// 对象的方法
	// 1,前面固定写减号-
	// 2,不同于函数,小括号()仅用来括住数据类型
	// 3,参数和返回值,都必须用小括号包起来(void)
	- (void)eat;
	@end
// 2,类的实现,专门用来实现成员方法
	@implementation Cat
	// - (void)eat;把声明复制过来,结尾的分号换成一对大括号
	- (void)eat
	{
		// 成员方法内直接访问成员变量
		NSLog(@"喵名:%s,毛色:%d,喵正在吃东西~",name,furColor);
	}
	@end

// 1,人类的声明
	@interface Person : NSObject
	{	
		// 所有成员变量,默认初始值均为0
		@public
		String name;
		Sex sex;
		int age;
		Date birthday;
		Color favouriteColor;
		// 对象的成员变量还是对象,OC任何对象都是指针指过去的~
		Cat *cat;
	}
	// 对象的方法
	// 1,前面固定写减号-
	// 2,不同于函数,小括号()仅用来括住数据类型
	// 3,参数和返回值,都必须用小括号包起来(void)
	- (void)toString;
	- (void)feedCat;
	// 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 *)other;
	@end
// 2,类的实现,专门用来实现成员方法
	@implementation Person
	// - (void)toString;把声明复制过来,结尾的分号换成一对大括号
	- (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->age;
	}
	@end
	
int main(){
	// 3,执行Car类的new行为,返回new出来的对象地址
	Person *p = [Person new];
	// 4,类似结构体访问,给成员赋值
	p->name = "pre_eminent";
	p->sex = SexBoy;
	p->age = 24;
	// 下面是错误的,结构体大括号这种赋值,只有在定义变量的时候才可以,类似的还有数组
	// p->birthday = {1990,6,7};
	Date d = {1990,6,7};
	// 同基本数据类型一样,结构体间可整体赋值(传值)
	p->birthday = d;
	// 指针访问结构体成员用->,结构体变量访问其成员用.
	p->birthday.month = 7;
	p->birthday.day = 6;
	p->favouriteColor = ColorBlue;
	// 6,执行行为,OC中对象仅能通过指针访问,
	// 给指针p所指向的对象,发送一条toString消息,消息机制
	[p toString];
	// 7,为成员变量cat赋值
	Cat *c = [Cat new];
	c->name = "Cafee";
	c->furColor = ColorGreen;
	// 将8个字节长的地址给了成员变量cat指针
	p->cat = c;
	[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];
	return 0;
}
================第2段代码(end)========================== = 
============第3段代码start=======================
#import <Foundation/Foundation.h>
// 定义枚举(类型前要有空格),类型为性别
typedef enum{
	SexBoy,SexGirl,SexUnknow
} Sex;
// 定义枚举(类型前要有空格),类型为颜色
typedef enum{
	ColorRed,ColorGreen,ColorBlue
} Color;
// 定义结构体(类型前要有空格),类型为日期时间型
typedef struct{
	int year;
	int month;
	int day;
} Date;
// 定义字符串类型,仿Java
typedef char *String;
// 1,喵类的声明
	@interface Cat : NSObject
	{	
		// 所有成员变量,默认初始值均为0
		// @public 以后所有成员变量均只通过set和get方法访问,且成员变量以_开头
		String _name;		
		Color _furColor;
	}
	// 类的方法注意事项:
	// 1,+号
	// 2,只能类调用,不能对象调用,因为+_号不同
	// 3,允许类方法与对象方法重名,因为还有+_号可以区分它俩
	// 4,同Java一样,类方法无法访问对象成员变量,因为类加载的时候,对象还没创建,而成员变量仅存在对象内存空间
	// 注:类内存空间只保存方法列表,即类的,对象的~
	+ (void)showClassName;
	// 对象的方法
	// 1,前面固定写减号-
	// 2,不同于函数,小括号()仅用来括住数据类型
	// 3,参数和返回值,都必须用小括号包起来(void)
	- (void)eat;
	// 4,_name的set方法
	- (void)setName:(String)name;
	// 5,_furColor的set方法
	- (void)setFurColor:(Color)furColor;
	// 6,_name的get方法,同Java不同,get方法名就是成员变量名
	- (String)name;
	// 7,_furColor的get方法,同Java不同,get方法名就是成员变量名
	- (Color)furColor;
	@end
// 2,类的实现,专门用来实现成员方法
	@implementation Cat
	// 类的方法实现
	+ (void)showClassName
	{
		NSLog(@"我是喵类");
	}
	// - (void)eat;把声明复制过来,结尾的分号换成一对大括号
	- (void)eat
	{
		// 成员方法内直接访问成员变量
		NSLog(@"喵名:%s,毛色:%d,喵正在吃东西~",_name,_furColor);
	}
	// _name的set方法
	- (void)setName:(String)name
	{
		_name = name;
	}
	// _furColor的set方法
	- (void)setFurColor:(Color)furColor
	{
		_furColor = furColor;
	}
	// _name的get方法
	- (String)name
	{
		return _name;
	}
	// _furColor的get方法
	- (Color)furColor
	{
		return _furColor;
	}
	@end

	
int main(){
	// 类的方法注意事项:
	// 1,+号
	// 2,只能类调用,不能对象调用,因为+_号不同
	// 3,允许类方法与对象方法重名,因为还有+_号可以区分它俩
	// 4,同Java一样,类方法无法访问对象成员变量,因为类加载的时候,对象还没创建,而成员变量仅存在对象内存空间
	// 注:类内存空间只保存方法列表,即类的,对象的~
	[Cat showClassName];

	Cat *c = [Cat new];
	[c setName:"Cafee"];
	[c setFurColor:ColorGreen];
	[c eat];
	NSLog(@"喵大名是:%s---颜色是:%d",[c name],[c furColor]);
	// 1,创建OC字符串方式一
	NSString *url = @"http:// www.baidu.com";
	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]);
	return 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);
        return ;
    }    
    // 代码能来到着,说明路径存在        
    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
            return ;
        }
        // 加载文件内容
        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);
	return 0;
}

H:/Objective-C/OC_day1/01-第1个OC程序.m
#include <stdio.h>

// OC程序的入口:main函数
int main()
{
    printf("第1个OC程序\n");
    return 0;
}

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程序!!!");
    
    return 0;
}

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));
    return 0;
}

H:/Objective-C/OC_day1/06-类的设计_头脑风暴.m
1.类的设计:
1> 类名
* 类名的第一个字母必须是大写
* 不能有下划线
* 多个英文单词,用驼峰标识
2> 属性
3> 行为(功能)

2.植物大战僵尸的僵尸
* 类名:Zoombie
* 属性:life、speed、gongjili
* 行为:walk、bite、die

3.雷电的飞机
* 类名:Plane
* 属性:life、gongjili、speed、bombCount
* 行为:fly、bomb、shoot、die

4.电脑
* 类名:Computer
* 属性:band、expireDate
* 行为:open、close

H:/Objective-C/OC_day1/07-第1个OC的类.m
/*
 类名:Car
 属性:轮胎个数、时速(速度)
 行为:跑
 */

// 因为使用了NSObject
#import <Foundation/Foundation.h>

// 完整地写一个函数:函数的声明和定义(实现)
// 完整地写一个类:类的声明和实现

// 1.类的声明
// 声明对象的属性、行为
// : NSObject 目的是:让Car这个类具备创建对象的能力
@interface Car : NSObject
{// 用来声明对象属性(实例变量\成员变量,默认会初始化为0)
    // @public可以让外部的指针间接访问对象内部的成员变量
    @public
    int wheels; // 轮胎个数
    int speed; // 时速(xxkm/h)
}

// 方法(行为):方法名、参数、返回值(声明、实现)
// 只要是OC对象的方法,必须以减号 - 开头
// OC方法中任何数据类型都必须用小括号()扩住
// OC方法中的小括号():括住数据类型
- (void)run;

@end

// 2.类的实现
// 用来实现@inteface中声明的方法
@implementation Car
// 方法的实现(说清楚方法里面有什么代码)

- (void)run
{
    NSLog(@"车子跑起来了");
}

@end

int main()
{
    // 在OC中,想执行一些行为,就写上一个中括号[行为执行者 行为名称]
    // 利用类来创建对象
    // 执行了Car这个类的new行为来创建新对象
    
    // 定义了一个指针变量p,p将来指向的是Car类型的对象
    // [Car new]每次都会创建出一个新对象,并且会返回新对象本身(新对象的地址)
    Car *p = [Car new];
    
    
    Car *p2 = [Car new];
    p2->wheels = 5;
    p2->speed = 300;
    [p2 run];
    
    // 给p所指向对象的wheels属性赋值
    p->wheels = 4;
    p->speed = 250;
    
    // 给p所指向对象发送一条run消息
    [p run];
    
    NSLog(@"车子有%d个轮子,时速位:%dkm/h", p->wheels, p2->speed);
    
    return 0;
}

H:/Objective-C/OC_day1/08-第2个OC的类.m
/*
 人
 类名:Person
 属性(成员变量\实例变量):体重、年龄
 行为(方法):走路、吃
 */
#import <Foundation/Foundation.h>
/*
 1.类的声明
    * 成员变量
    * 方法的声明
 */
@interface Person : NSObject
{
    @public
    int age;
    double weight;
}

- (void)walk;
- (void)eat;

@end


// 2.类的实现
@implementation Person

// 实现@interface中声明的方法
- (void)walk
{
    NSLog(@"%d岁、%f公斤的人走了一段路", age, weight);
}

- (void)eat
{
    NSLog(@"%d岁、%f公斤的人在吃东西", age, weight);
}

@end

int main()
{
    // 在使用类创建对象之前,会将类加载进内存
    Person *p = [Person new];
    p->age = 20;
    p->weight = 40;
    
    [p eat];
    [p walk];
    
    Person *p2 = [Person new];
    p2->age = 30;
    p2->weight = 60;
    [p2 eat];
    [p2 walk];
    
    /*
    Person *p2 = [Person new];
    p2->age = 30;
    p2->weight = 50;
    
    p = p2;
    
    p->age = 40;
    
    [p2 walk];
    */
    
    /*
    Person *p = [Person new];
    p->age = 20;
    
    Person *p2 = [Person new];
    p2->weight = 50.0;
    
    [p walk];
     */
     
    /*
    Person *p = [Person new];
    p->age = 20;
    p->weight = 50.0;
    [p walk];
    
    Person *p2 = [Person new];
    p2->age = 30;
    p2->weight = 60.0;
    [p2 walk];
    */
    return 0;
}

H:/Objective-C/OC_day1/09-OC对象跟函数.m
#import <Foundation/Foundation.h>

@interface Car : NSObject
{// 成员变量
    @public
    int wheels;
    int speed;
}

- (void)run;
@end

@implementation Car
- (void)run
{
    NSLog(@"%d个轮子,速度为%dkm/h的车子跑起来", wheels, speed);
}
@end


void test(int w, int s)
{
    w = 20;
    s = 200;
}

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);
    test2(c);
    
    [c run];
    
    return 0;
}

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
@end

@implementation Person
@end

@interface Car : NSObject
{// 成员变量\实例变量
    //int wheels = 4; 不允许在这里初始化
    //static int wheels; 不能随便将成员变量当做C语言中的变量来使用
    @public
    int wheels;
}

- (void)run;
- (void)fly;
@end

int main()
{
    // wheels = 10;
    /*
    Car *c = [Car new];
    c->wheels = 4;
    //run();
    
    [c run];
    */
    
    void test2();
    
    test2();
    
    return 0;
}

@implementation Car

- (void) fly
{
    
}

/*
void test2()
{
    NSLog(@"调用了test2函数-%d", wheels);
}*/

void test()
{
    NSLog(@"调用了test函数");
}

- (void)run
{
    test();
    NSLog(@"%d个轮子的车跑起来了", wheels);
}
@end

H:/Objective-C/OC_day1/11-类的合理设计.m
#import <Foundation/Foundation.h>

typedef enum {
    SexMan,
    SexWoman
} Sex;

typedef struct {
    int year;
    int month;
    int day;
} Date;


typedef enum {
    ColorBlack,
    ColorRed,
    ColorGreen
} Color;

@interface Dog : NSObject
{
    @public
    double weight; // 体重
    Color curColor; // 毛色
}

- (void)eat;
- (void)run;
@end

@implementation Dog
- (void)eat
{
    // 每吃一次,体重就加1
    weight += 1;
    //weight = weight +  1;
    NSLog(@"狗吃完这次后的体重是%f", weight);
}

- (void)run
{
    weight -= 1;
    NSLog(@"狗跑完这次后的体重是%f", weight);
}
@end

/*
学生
成员变量:性别、生日、体重、最喜欢的颜色、狗(体重、毛色,吃、跑)
方法:吃、跑步、遛狗(让狗跑)、喂狗(让狗吃)
*/
@interface Student : NSObject
{
    @public
    Sex sex; // 性别
    Date birthday; // 生日
    double weight; // 体重(kg)
    Color favColor; // 最喜欢的颜色
    char *name;
    
    // 重点:狗
    Dog *dog;
}
- (void)eat;
- (void)run;
- (void)print;

- (void)liuDog;
- (void)weiDog;
@end

@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 +  1;
    NSLog(@"学生吃完这次后的体重是%f", weight);
}

- (void)run
{
    weight -= 1;
    NSLog(@"学生跑完这次后的体重是%f", weight);
}
@end

int main()
{
    Student *s = [Student new];
    
    Dog *d = [Dog new];
    d->curColor = ColorGreen;
    d->weight = 20;
    s->dog = d;
    
    
    [s liuDog];
    
    [s weiDog];
    return 0;
}


void test()
{
    Student *s = [Student new];
    s->weight = 50;
    
    // 性别
    s->sex = SexMan;
    
    // 生日
    Date d = {2011, 9, 10};
    s->birthday = d;
    
    s->name = "Jack";
    
    /*
     s->birthday.year = 2011;
     s->birthday.month = 9;
     s->birthday.day = 10;
     */
    
    // 喜欢的颜色
    s->favColor = ColorBlack;
    /*
     [s eat];
     [s eat];
     
     [s run];
     [s run];
     */
    
    [s print];
}


H:/Objective-C/OC_day1/12-方法的声明和实现.m
#import <Foundation/Foundation.h>

/*
计算器类
 方法:
 1> 返回 π 
 2> 计算某个整数的平方
 3> 计算两个整数的和
*/

@interface JiSuanQi : NSObject

// 方法名:pi
- (double)pi;

// OC方法中,一个参数对应一个冒号
// 方法名:pingFang:(冒号也是方法名的一部分)
- (int)pingFang:(int)num;

//- (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;
@end

@implementation JiSuanQi

- (double)pi
{
    return 3.14;
}

- (int)pingFang:(int)num
{
    return num * num;
}

//- (int)sum:(int)num1 :(int)num2
- (int)sumWithNum1:(int)num1 andNum2:(int)num2
{
    return num1 + num2;
}
@end

int main()
{
    JiSuanQi *jsq = [JiSuanQi new];
    
    
    int a = [jsq sumWithNum1:20 andNum2:5];
    //int a = [jsq sum:10 :5];
    
    //int a =  [jsq pingFang:10];
    
    //double a = [jsq pi];
    NSLog(@"%i", a);
    
    return 0;
}

H:/Objective-C/OC_day1/13-练习.m
/*
设计一个方法,用来和其他车比较车速,返回车速的差距
 */

#import <Foundation/Foundation.h>

@interface Car : NSObject
{
    @public
    int speed;
}

- (int)compareSpeedWithOther:(Car *)other;

@end

@implementation Car

- (int)compareSpeedWithOther:(Car *)other
{
    // speed
    // other->speed
    // 返回当前这辆车和other这辆车的速度差距
    return speed - other->speed;
}

@end

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);
    
    return 0;
}

H:/Objective-C/OC_day1/14-匿名对象.m
#import <Foundation/Foundation.h>
@interface Car : NSObject
{
    @public
    int speed;
}
- (void)run;
@end

@implementation Car

- (void)run
{
    NSLog(@"速度为%d的车子跑起来了", speed);
}

@end


int main()
{
    /*
    Car *c;
    c = [Car new];
    c->speed = 250;
    
    [c run];*/
    
    // 不要写类似匿名对象这样的代码
    // 只要求能说出输出结果
    [Car new]->speed = 300;
    
    [[Car new] run];
    
    //Car *c = [Car new];
    
    return 0;
}

H:/Objective-C/OC_day1/15-01-改错题-答案.m
第1题:类的声明和定义、对象的创建
//#import <Foundation.h>  前面要加上框架名称"Foundation/"
#import <Foundation/Foundation.h>
@interface Person : NSObject
{
    // @public能让外面的指针访问对象内部的成员变量
    @public
    int age;
    // 成员变量不能在类声明中进行初始化
    //double height = 1.55;
    double height;
    
    // 方法的声明不能写在{}中
    // - (void)study;
}
- (void)study;
// 每个@interface都必须和一个@end配对
@end

@implementation Person
- (void)study
{
    NSLog(@"年龄为%d的人在学习", age);
}
@end

int main()
{
    // 要利用指针变量来指向对象
    //Person p = [Person new];
    Person *p = [Person new];
    p->age = 10;
    p->height = 1.78f;
    [p study];
    return 0;
}
// 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)pi;

// 小括号()在方法中只用来括住具体类型
//- (void)test();
- (void)test;
@end

@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
{
    
}
@end

int main()
{
    return 0;
}

// 6处错误


第3题:方法和函数的区别
#import <Foundation/Foundation.h>
@interface Car : NSObject
{
    @public
    int wheels;
}

- (void)run;
- (void)test;
@end

@implementation Car
- (void)test
{
    NSLog(@"测试一下车子:%i", wheels);
}

// OC方法的实现跟函数是不一样的
// void run()
- (void)run
{
    NSLog(@"%i个轮子的车跑起来了", wheels);
}
@end

// 方法不能独立于类存在,类外面只能定义函数
//- (void)haha
void haha()
{
    NSLog(@"调用了haha");
}

int main()
{
    Car *c = [Car new];
    [c run];
    [c test];
    
    // OC方法不能当做函数来调用,需要用对象来调用
    // test();
    haha();
    return 0;
}
// 3处错误

H:/Objective-C/OC_day1/15-02-分析题-答案-.m
第1题:对象的操作、对象作为函数参数
#import <Foundation/Foundation.h>
@interface Person : NSObject
{
    @public
    int age;
    double height;
}
- (void)print;
@end

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];
    
    test2(p);
    [p print];
    
    test3(p);
    [p print];
    
    test4(p);
    [p print];
    
    return 0;
}

@implementation Person
- (void)print
{
    NSLog(@"年龄=%d,身高=%f", age, height);
}
@end

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
{
    @public
    int age;
    double height;
}
- (void)print;
@end

@implementation Person
- (void)print
{
    NSLog(@"年龄=%d,身高=%f", age, height);
}
@end

int main()
{
    [Person new]->age = 10;
    [Person new]->height = 1.8;
    [[Person new] print];
    return 0;
}

=======================================
第2题:匿名对象
年龄=0,身高=0.000000
=======================================

H:/Objective-C/OC_day1/15-03-类的设计-Dog.m
/*
 设计一个”狗“类
 1> 属性
 * 颜色
 * 速度(单位是m/s)
 * 性别
 * 体重(单位是kg)
 
 2> 行为
 * 吃:每吃一次,体重增加0.5kg,输出吃完后的体重
 * 吠(叫):输出所有的属性
 * 跑:每跑一次,体重减少0.5kg,输出速度和跑完后的体重
 * 比较颜色:跟别的狗比较颜色,如果一样,返回YES,不一样,返回NO
 * 比较速度:跟别的狗比较速度,返回速度差(自己的速度 - 其他狗的速度)
*/

#import <Foundation/Foundation.h>

// 颜色
typedef enum {
    ColorBlack,
    ColorGreen,
    ColorBlue,
    ColoeRed
} Color;

// 性别
typedef enum {
    SexMan,
    SexWoman,
    SecUnknow
} Sex;

@interface Dog : NSObject
{
    @public
    Color color; // 颜色
    int speed; // 速度
    Sex sex; // 性别
    double weight; // 体重
}

- (void)eat; // 吃
- (void)bark; // 吠
- (void)run; // 跑
- (BOOL)isSameColorWithOther:(Dog *)other; // 比较是否为相同颜色
- (int)compareSpeedWithOther:(Dog *)other; // 比较速度差
@end

@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->color;
}
- (int)compareSpeedWithOther:(Dog *)other // 比较速度差
{
    return speed - other->speed;
}
@end

H:/Objective-C/OC_day1/15-04-类的设计-Person.m
/*
 2.结合前面的“狗”类,设计一个“人”类
 1> 属性
 * 姓名
 * 狗(养了一条狗)
 
 2> 行为
 * 喂狗:每喂一次,狗就会执行“吃”这个行为
 * 遛狗:每溜一次,狗就会执行“跑”这个行为
*/
#import <Foundation/Foundation.h>

// 颜色
typedef enum {
    ColorBlack,
    ColorGreen,
    ColorBlue,
    ColoeRed
} Color;

// 性别
typedef enum {
    SexMan,
    SexWoman,
    SecUnknow
} Sex;

/*********************狗********************/
@interface Dog : NSObject
{
@public
    Color color; // 颜色
    int speed; // 速度
    Sex sex; // 性别
    double weight; // 体重
}

- (void)eat; // 吃
- (void)bark; // 吠
- (void)run; // 跑
- (BOOL)isSameColorWithOther:(Dog *)other; // 比较是否为相同颜色
- (int)compareSpeedWithOther:(Dog *)other; // 比较速度差
@end

@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->color;
}
- (int)compareSpeedWithOther:(Dog *)other // 比较速度差
{
    return speed - other->speed;
}
@end

/*********************人********************/
@interface Person : NSObject
{
    @public
    char *name; // 姓名
    Dog *dog; // 狗
}
- (void)walkDog; // 遛狗
- (void)feedDog; // 喂狗
@end

@implementation Person
- (void)walkDog // 遛狗
{
    [dog run];
}
- (void)feedDog // 喂狗
{
    [dog eat];
}
@end

H:/Objective-C/OC_day1/15-05-类的设计-Studeng类1.m
/*
 3.设计一个”学生“类
 1> 属性
 * 姓名
 * 生日
 * 年龄
 * 身高(单位是m)
 * 体重(单位是kg)
 * 性别
 * C语言成绩
 * OC成绩
 * iOS成绩
 
 2> 行为
 * 跑步:每跑步一次,身高增加1cm,体重减小0.5kg,输出跑完后的体重
 * 吃饭:每吃一次,身高增加1cm,体重增加0.5kg,输出吃完后的体重
 * 学习:每学习一次,3可成绩各加1分,输出学习完后的3科成绩
 * 睡觉:输出所有的属性
 * 比较C语言成绩:跟另外一个学生比较C语言成绩,返回成绩差(自己的成绩 - 其他人的成绩)
 * 比较OC成绩:跟另外一个学生比较OC语言成绩,返回成绩差(自己的成绩 - 其他人的成绩)
 * 比较iOS成绩:跟另外一个学生比较iOS语言成绩,返回成绩差(自己的成绩 - 其他人的成绩)
 * 计算总分:算出3科成绩的总分
 * 计算平均分:算出3科成绩的平均分
*/

#import <Foundation/Foundation.h>
// 颜色
typedef struct {
    int year;
    int month;
    int day;
} Date;

// 性别
typedef enum {
    SexMan,
    SexWoman,
    SecUnknow
} Sex;

@interface Student : NSObject
{
    @public
    char *name; // 姓名
    Date birthday; // 生日
    int age; // 年龄
    double height; // 身高
    double weight; // 体重
    Sex sex; // 性别
    int cScore; // C语言成绩
    int ocScore; // OC成绩
    int iosScore; // iOS成绩
}

- (void)run; // 跑
- (void)eat; // 吃
- (void)study; // 学习
- (void)sleep; // 睡觉
- (int)compareCScoreWithOther:(Student *)other; // 比较C成绩
- (int)compareOcScoreWithOther:(Student *)other; // 比较OC成绩
- (int)compareIosScoreWithOther:(Student *)other; // 比较iOS成绩
- (int)totalScore; // 总分
- (int)averageScore; // 平均分
@end

@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->cScore;
}
- (int)compareOcScoreWithOther:(Student *)other // 比较OC成绩
{
    return ocScore - other->ocScore;
}
- (int)compareIosScoreWithOther:(Student *)other // 比较iOS成绩
{
    return iosScore - other->iosScore;
}
- (int)totalScore // 总分
{
    return cScore + ocScore + iosScore;
}
- (int)averageScore // 平均分
{
    return (cScore + ocScore + iosScore) / 3;
}
@end

H:/Objective-C/OC_day1/15-06-类的设计-成绩类.m
/*
 4.设计一个成绩类
 1> 属性
 * C语言成绩
 * OC成绩
 * iOS成绩
 
 2> 行为
 * 比较C语言成绩:跟另外一个成绩对象比较C语言成绩,返回成绩差(自己 - 其他成绩)
 * 比较OC成绩:跟另外一个成绩对象比较OC语言成绩,返回成绩差(自己 - 其他成绩)
 * 比较iOS成绩:跟另外一个成绩对象比较iOS语言成绩,返回成绩差(自己 - 其他成绩)
 * 计算总分:算出3科成绩的总分
 * 计算平均分:算出3科成绩的平均分
*/

#import <Foundation/Foundation.h>

@interface Score : NSObject
{
    @public
    int cScore; // C语言成绩
    int ocScore; // OC成绩
    int iosScore; // iOS成绩
}

- (int)compareCScoreWithOther:(Score *)other; // 比较C成绩
- (int)compareOcScoreWithOther:(Score *)other; // 比较OC成绩
- (int)compareIosScoreWithOther:(Score *)other; // 比较iOS成绩
- (int)totalScore; // 总分
- (int)averageScore; // 平均分
@end

@implementation Score
- (int)compareCScoreWithOther:(Score *)other // 比较C成绩
{
    return cScore - other->cScore;
}
- (int)compareOcScoreWithOther:(Score *)other // 比较OC成绩
{
    return ocScore - other->ocScore;
}
- (int)compareIosScoreWithOther:(Score *)other // 比较iOS成绩
{
    return iosScore - other->iosScore;
}
- (int)totalScore // 总分
{
    return cScore + ocScore + iosScore;
}
- (int)averageScore // 平均分
{
    return (cScore + ocScore + iosScore) / 3;
}
@end

H:/Objective-C/OC_day1/15-07-类的设计-Student类2.m
/*
 5.利用前面的成绩类,重新设计一个学生类
 1> 属性
 * 姓名
 * 学号
 * 成绩(包括3科成绩)
 
 2> 行为
 * 比较C语言成绩:跟另外一个学生比较C语言成绩,返回成绩差(自己的成绩 - 其他人的成绩)
 * 比较OC成绩:跟另外一个学生比较OC语言成绩,返回成绩差(自己的成绩 - 其他人的成绩)
 * 比较iOS成绩:跟另外一个学生比较iOS语言成绩,返回成绩差(自己的成绩 - 其他人的成绩)
 * 比较总分:跟另外一个学生比较总分,返回成绩差(自己的成绩 - 其他人的成绩)
 * 比较平均分:跟另外一个学生比较平均分,返回成绩差(自己的成绩 - 其他人的成绩)
*/

#import <Foundation/Foundation.h>
/****************成绩******************/
@interface Score : NSObject
{
    @public
    int cScore; // C语言成绩
    int ocScore; // OC成绩
    int iosScore; // iOS成绩
}

- (int)compareCScoreWithOther:(Score *)other; // 比较C成绩
- (int)compareOcScoreWithOther:(Score *)other; // 比较OC成绩
- (int)compareIosScoreWithOther:(Score *)other; // 比较iOS成绩
- (int)totalScore; // 总分
- (int)averageScore; // 平均分
@end

@implementation Score
- (int)compareCScoreWithOther:(Score *)other // 比较C成绩
{
    return cScore - other->cScore;
}
- (int)compareOcScoreWithOther:(Score *)other // 比较OC成绩
{
    return ocScore - other->ocScore;
}
- (int)compareIosScoreWithOther:(Score *)other // 比较iOS成绩
{
    return iosScore - other->iosScore;
}
- (int)totalScore // 总分
{
    return cScore + ocScore + iosScore;
}
- (int)averageScore // 平均分
{
    return (cScore + ocScore + iosScore) / 3;
}
@end

/********************学生********************/
@interface Student : NSObject
{
    @public
    char *name; // 姓名
    int no; // 学号
    Score *score; // 成绩
}

- (int)compareCScoreWithOther:(Student *)other; // 比较C成绩
- (int)compareOcScoreWithOther:(Student *)other; // 比较OC成绩
- (int)compareIosScoreWithOther:(Student *)other; // 比较iOS成绩
- (int)compareTotalScoreWithOther:(Student *)other; // 比较总成绩
- (int)compareAverageScoreWithOther:(Student *)other; // 比较平均成绩
@end

@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];
}
@end

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值