(一)概述
OC 对象的本质就是结构体。类的声明要放前面,类的实现可以放后面;同时要注意方法和函数(OC完全兼容C)的区别:方法的实现只能写在@implementation里面,而函数可以写在任意位置(归文件所有)且它的调用不依赖于对象。
ios-oop.png(二)类的声明和实现
#import <Foundation/Foundation.h>
// 类的声明
// : NSObject 目的是让 Car 具备创建对象能力
@interface Car : NSObject {
@public// @public 让外部指针允许访问对象内部成员
int wheels;// 默认为0(不允许在这初始化)
double speed;
}
// 方法声明(必须以减号 - 开头)
// 注意:任何数据类型必须加(),且仅加在数据类型上
- (void)driver;
@end
// 类的实现(实现声明的方法)
@implementation Car
- (void)driver {
NSLog(@"%d个轮子,速度%fkm/h的车子正在驾驶中...",wheels,speed);
}
@end
// 函数传值
void testValue(int wheels,double speed) {
wheels = 10;
speed = 200.0;
}
// 函数传地址
void testPoint(Car *car) {
car->wheels = 10;
car->speed = 200.0;
}
// [对象 方法]
int main(int argc, const char * argv[]) {
// [Car new] 创建一个 Car 对象并返回该对象本身(地址)
// 指针 p 指向 Car 类型的对象
Car *p = [Car new];
// 将指针 p 赋值给指针 p2
Car *p2 = p;
p->wheels = 4;
p->speed = 120.0;
NSLog(@"wheels=%d,speed=%f,%p,%p",p2->wheels,p2->speed,p2,p);
[p2 driver];// 调用run方法
// ============= 函数 ============//
// 传值,属性不变
testValue(p->wheels,p->speed);
[p driver];
// 传地址,属性改变
testPoint(p);
[p driver];
return 0;
}
(三)枚举和结构体
#import <Foundation/Foundation.h>
typedef enum {
SexMan,
SexWoman
} Sex;
typedef struct {
int year;
int month;
int day;
} Date;
typedef enum {
ColorRed,
ColorGreen,
ColorBlue
} Color;
// Dog
@interface Dog: NSObject {
@public
double weight;
Color color;
char *name;
}
- (void)eat;
- (void)run;
@end
@implementation Dog
- (void)eat {
weight++;
NSLog(@"Dog eat...weight=%f",weight);
}
- (void)run {
weight--;
NSLog(@"Dog run...weight=%f",weight);
}
@end
// Person
@interface Person : NSObject{
@public
Sex sex;
Date birthday;
double weight;
Color favorColor;
char *name;
Dog *dog;// 使用指针
}
- (void)eat;
- (void)run;
- (void)print;
- (void)walkDog;
- (void)feedDog;
@end
@implementation Person
- (void)eat {
weight++;
NSLog(@"Person eat...weight=%f",weight);
}
- (void)run {
weight--;
NSLog(@"Person run...weight=%f",weight);
}
- (void)print {
NSLog(@"sex=%d,birthday=%d-%d-%d,weight=%f,favorColor=%d, name=%s,Dog(weight=%f,color=%d,name=%s)",sex,birthday.year,birthday.month,birthday.day,weight,favorColor,name,dog->weight,dog->color,dog->name);
}
- (void)walkDog {
[dog run];
}
- (void)feedDog {
[dog eat];
}
@end
int main() {
Person *s = [Person new];
s->weight = 100.0;
s->sex = SexWoman;
// Date 第一种写法(推荐):
Date d = {2022,12,14};
s->birthday = d;
// Date 第二种写法:
s->birthday.year = 2022;
s->birthday.month = 12;
s->birthday.day = 14;
s->favorColor = ColorRed;
s->name = "CloudKK";
// Dog
Dog *dog = [Dog new];
dog->weight = 30.0;
dog->color = ColorGreen;
dog->name = "XiaoHei";
s->dog = dog;
[s eat];
[s run];
[s walkDog];
[s feedDog];
// sex=1,birthday=2022-12-14,weight=100.000000,favorColor=0, name=CloudKK,Dog(weight=30.000000,color=1,name=XiaoHei)
[s print];
return 0;
}
(四)普通类型参数
在OC方法中,一个参数对应一个冒号。
#import <Foundation/Foundation.h>
@interface Calculator : NSObject
- (double)pi;
// 在OC方法中,一个参数对应一个冒号
- (int)square : (int)num;
// 方法名 sum:andNum2: 注意这里的冒号也是它的一部分
// 其中 andNum2 为描述符(可不写)
- (int)sum : (int)num1 andNum2 : (int)num2;
- (int)sumSquare : (int)num1 : (int)num2;
@end
@implementation Calculator
- (double)pi {
return 3.14;
}
- (int)square : (int)num {
return num * num;
}
- (int)sum : (int)num1 andNum2 : (int)num2 {
return num1 + num2;
}
- (int)sumSquare : (int)num1 : (int)num2 {
return num1 * num1 + num2 * num2;
}
@end
int main() {
Calculator *c = [Calculator new];
double pi = [c pi];
NSLog(@"pi=%f",pi);
int square = [c square:9];
NSLog(@"square=%d",square);
int sum = [c sum:9 andNum2:10];
NSLog(@"sum=%d",sum);
int sumSquare = [c sumSquare:9: 10];
NSLog(@"sumSquare=%d",sumSquare);
return 0;
}
(五)指针类型参数
#import <Foundation/Foundation.h>
@interface Car : NSObject {
@public
int speed;
}
// 传指针类型参数
- (int)compareSpeedWithOther : (Car *)other;
@end
@implementation Car
- (int)compareSpeedWithOther : (Car *)other {
return speed - other->speed;
}
@end
int main() {
Car *car1 = [Car new];
car1->speed = 100;
Car *car2 = [Car new];
car2->speed = 120;
int result = [car1 compareSpeedWithOther: car2];
NSLog(@"result=%d",result);
// 匿名对象(不推荐)
// [Car new]->speed = 100;
// [[Car new] run];
return 0;
}
网友评论