@fiy-fish
2015-05-19T09:36:38.000000Z
字数 2965
阅读 1587
Objective-c
//// main.m// 构造方法重载//// Created by Aaron on 15/5/19.// Copyright (c) 2015年 Aaron. All rights reserved.//#import <Foundation/Foundation.h>/*类是用来描述对象的对象是由类产生的一个实体封装1.找对象2.分析属性3.分析行为4.创建类-----接口部分,实现部分接口部分:定义属性 声明方法实现部分:实现方法5.通过类创建对象6.利用对象完成功能@interface 类名:NSObject{默认保护类型:只能在当前类或者其子类直接访问私有:只能在当前类里面直接访问公有:任何地方都可以直接访问(最不安全,违背了封装的思想)定义属性(实例变量)}定义方法-表示实例方法-(void)test;-(void)test:(int)num;OC没有私有类型的方法但是可以通过不提供接口的方式来实现方法的私有化提供setter getter接口来实现对属性的访问-(void)set实例变量名:(类型)参数;-(类型)实例变量名;@end@implementation 类名实现方法-(int)test{static int i = 100;return i;}@end*/#import "Shoose.h"#import "Dog.h"int main(int argc, const char * argv[]){@autoreleasepool {Shoose *s = [[Shoose alloc] init];[s showMe];Shoose *s1 = [[Shoose alloc] initWithColor:@"黑色"];[s1 showMe];Dog *dog1 = [[Dog alloc] initWithSpeed:99];Dog *dog2 = [[Dog alloc] initWithSpeed:200];NSLog(@"%d",[dog1 compareWithOtherDog:dog2]);}return 0;}
// Shoose.h// 构造方法重载//// Created by Aaron on 15/5/19.// Copyright (c) 2015年 Aaron. All rights reserved.//#import <Foundation/Foundation.h>@interface Shoose : NSObject{NSString *_color;}-(void)showMe;//-(id)init; 如果是系统的init方法,可以不用声明//如果是自己重载的构造方法,需要声明-(id)initWithColor:(NSString *)color;@end
// Shoose.m// 构造方法重载//// Created by Aaron on 15/5/19.// Copyright (c) 2015年 Aaron. All rights reserved.//#import "Shoose.h"@implementation Shoose//id泛型的对象指针-(id)init{if(self = [super init]){_color = @"白色";}return self;}-(id)initWithColor:(NSString *)color{if(self = [super init]){_color = color;}return self;}-(void)showMe{//%@输出OC的字符串NSLog(@"这是一个%@的鞋子",_color);}@end
// Dog.h// 构造方法重载//// Created by Aaron on 15/5/19.// Copyright (c) 2015年 Aaron. All rights reserved.//#import <Foundation/Foundation.h>/*设计一个狗类Dog,给类添加一个方法,该方法用来比较狗A与另外一只狗B的速度,如果A的速度快,返回1,如果B的速度快,返回-1,相同则返回0.*/@interface Dog : NSObject{int _speed;int _weight;}//-(void)setSpeed:(int)speed;//-(void)setWeight:(int)weight;-(void)setSpeed:(int)speed andWeight:(int)weight;-(id)initWithSpeed:(int)speed;//带多个参数的方法//:表示一个参数 有多个参数就要带多个://withWeight 叫做标签//initWithSpeed:withWeight:-(id)initWithSpeed:(int)speed withWeight:(int)weight;-(int)compareWithOtherDog:(Dog *)otherDog;@end
// Dog.m// 构造方法重载//// Created by Aaron on 15/5/19.// Copyright (c) 2015年 Aaron. All rights reserved.//#import "Dog.h"@implementation Dog-(id)init{// if(self = [super init])// {// _speed = 10000;// }self = [self initWithSpeed:10000 withWeight:0];return self;}-(id)initWithSpeed:(int)speed{// if(self = [super init])// {// _speed = speed;// }self = [self initWithSpeed:speed withWeight:0];return self;}-(id)initWithSpeed:(int)speed withWeight:(int)weight{if(self = [super init]){_speed = speed;_weight = weight;}return self;}-(int)compareWithOtherDog:(Dog *)otherDog{//在类的内部,用对象调用实例方法[otherDog test1];//也可以通过self指针来调用//要看取到谁的实例变量,看谁是消息的接收者[self test1];if(_speed > otherDog->_speed){return 1;}else if(_speed == otherDog->_speed){return 0;}else{return -1;}}-(void)test1{NSLog(@"test1 speed = %d",_speed);}-(void)setSpeed:(int)speed{if(_speed != speed){_speed = speed;}}-(void)setWeight:(int)weight{if(_weight != weight){_weight = weight;}}-(void)setSpeed:(int)speed andWeight:(int)weight{[self setSpeed:speed];[self setWeight:weight];}@end