main.m
#import "Person.h"
#import "Car.h"
#import <Foundation/Foundation.h>
/**
* 面向过程:以过程为核心,注重完成事件的详细步骤,一步一步如何实现.
面向对象:以实物为核心,注重的是参与该事件的事物应该具备的功能.而完成该事件只是事物所有功能中的一个功能.
OO:(Object Oriented)面向对象
OOP:(Object Oriented Progranmming)面向对象编程
类:具有相同特征以及行为的抽象.它是一个抽象的概念,不具体.
对象:类的实例,类的具体体现,生活中的万物都是对象.
如何区分类与对象.
*
*/
int main(int argc, const char * argv[])
{
@autoreleasepool {
//如何通过类来创建对象
//创建对象一共分两步:1.开辟空间(堆区) 2.初始化
//OC中的方法的调用形式.消息发送机制 [receiver message]
// 接收 消息
// receiver(接收) ---->类(+), 对象(-)
// message(方法) ----> +, -
// -方法(对象方法,实例方法)
// +方法(类方法)
//id等同于void *,泛型,可以代表所有的对象.
//因为per存储着对象堆区空间的地址,所以间接把per叫做对象,但是本质上是指针变量.
/*
Person *per = [Person alloc]; //开辟空间
per = [per init];//初始化
*/
// Person *per = [[Person alloc] init];
// NSLog(@"%@", per); //%@ 打印对象
//<Person: 0x100201d80> Person 代表当前对象所属的类,0x100201d80代表当前对象的地址
//特例,初始化失败,返回空
NSTimer *timer = [[NSTimer alloc] init];
NSLog(@"timer = %@", timer);
/*
Person *per = [[Person alloc] init];
[per sayHello]; //调用sayHello
NSInteger money = [per getMoney];
NSLog(@"money = %ld", money);
NSInteger sum = [per sumValueWithA:10 b:5 c:20];
NSLog(@"sum = %ld", sum);
[per output:100];
NSInteger minGb = [per minGbWithX:12 y:15];
NSLog(@"minGb = %ld", minGb);
Student *stu = [[Student alloc] init];
[stu sayGoodbye];
NSInteger number = [stu number];
NSInteger maxscore = [stu maxScoreWithA:95 b:97];
Car *bus = [[Car alloc] init];
[bus run];
[bus manned];
Phone *ph = [[Phone alloc] init];
[ph output:@"string"];
NSInteger pn = [ph phoneNumber:01035471];
*/
//访问实例变量
//实例变量name是受到保护的,不能访问
Person *per = [[Person alloc] init];
per->name = @"hello";
per->gender = @"man";
per->age = 28;
per->height = 2000;
//输出实例变量的值
NSLog(@"name = %@", per->name);
NSLog(@"gender = %@", per->gender);
NSLog(@"age = %ld", per->age);
NSLog(@"height = %.1f", per->height);
Student *stu = [[Student alloc] init];
stu->name = @"Sherlock";
stu->gender = @"man";
stu->age = 56;
stu->score = 300;
NSLog(@"name = %@", stu->name);
NSLog(@"gender = %@", stu->gender);
NSLog(@"age = %ld", stu->age);
NSLog(@"score = %.2f", stu->score);
Car *bus = [[Car alloc] init];
bus->brand = @"random";
bus->color = @"blue";
bus->price = 100000000;
NSLog(@"brand = %@", bus->brand);
NSLog(@"color = %@", bus->color);
NSLog(@"price = %.2f", bus->price);
/**
* 规范
1.类名:由英文单词组成,每个单词首字母都要大写,不能出现数字.
2.实例变量,方法名:也是由英文单词组成,除了第一个单词的首字母小写外,其他单词首字母大写.
3.一般情况下一对.h与.m文件中定义一个类.
*/
//@是OC的标志
//输出的目的:验证.
/*
NSLog(@"Hello, World!");
int a = 10 + 5;
NSLog(@"a = %d", a);
NSLog(@"lanou");
*/
}
return 0;
}
Person.h
#import <Foundation/Foundation.h>
//NSObject 是OC中唯一一个没有父类的类.叫做根类.
/**
* 类的定义分为两部分:1.接口部分(.h文件中) 2.实现部分(.m文件中)
接口部分:以@interface 开头 + 类名 :(冒号表示继承) 父类名 @end 作为结束.
类的接口部分的内容必须写在@interface 与 @end 之间.
类的接口部分提供实例变量以及方法的声明.
类的本质就是一种自定义的数据类型.
*/
@interface Person : NSObject
{
//实例变量三种可见度@private @protected @public
//默认可见度是@protected
// 访问权限 | @private | @protected | @public
// | | |
// 本类文件 | YES 直接访问 | YES 直接访问 | YES 直接访问
// | | |
// 子类文件 | NO 不能访问 | YES 直接访问 | YES 直接访问
// | | |
// 其他文件 | NO 不能访问 | NO 不能访问 | YES 对象指向操作符
// | | | (间接访问)
@public //定义为公共的
//实例变量一定要写在大括号之内,而且大括号之内也只能写实例变量.
//对应类的特征,实例变量(等同结构体的结构体成员)
//姓名
NSString *name;
//性别
NSString *gender;
//年龄
NSInteger age;
//身高
CGFloat height;
}
// ----- 类的行为 ----- 方法(函数)
//void sayHello(); ---- C语言的函数
- (void)sayHello;//OC的方法 方法名:sayHello
//int getMoney();
- (NSInteger)getMoney; //有返回值,无参 方法名:getMoney
//void output(int n);
- (void)output:(NSInteger)n; //无返回值,有参 方法名:output:
//int sumValue(int a, int b, int c);
- (NSInteger)sumValueWithA:(NSInteger)a b:(NSInteger)b c:(NSInteger)c; //有返回值,有参
// 方法名: sumValueWithA:b:c:
//int maxGy(int x, int y);
- (NSInteger)maxGyWithX:(NSInteger)x y:(NSInteger)y; //方法名:maxGyWithX:y:
//int minGb(int x, int y);
- (NSInteger)minGbWithX:(NSInteger)x y:(NSInteger)y; //方法名:minGbWithX:y:
@end
//Student类的接口部分.
@interface Student : NSObject
{
@public
NSString *name;
NSString *gender;
NSInteger age;
CGFloat score;
}
- (void)sayGoodbye;
- (NSInteger)number;
- (NSInteger)maxScoreWithA:(NSInteger)a b:(NSInteger)b;
@end
@interface Phone : NSObject
{
NSString *color;
NSString *brand;
NSString *number;
}
- (void)output:(NSString*)a;
- (NSInteger)phoneNumber:(NSInteger)n;
@end
//文件与类的关系
//文件里面存放类的定义(接口部分.h 实现部分.m)
//文件中可以存放多个类.一般情况下一对.h与.m文件中只存放一个类.
Person.m
#import "Person.h"
/**
* 类的实现部分:以 @implementation 开头 + 实现类名 @end 作为实现部分的结束.
类的实现代码必须写在@implementation 与 @end 之间才有效.
类的实现主要是方法的实现.
*/
@implementation Person
- (void)sayHello {
NSLog(@"hello world");
}
- (NSInteger)getMoney{
return 100;
}
- (void)output:(NSInteger)n{
NSLog(@"n = %ld", n);
}
- (NSInteger)sumValueWithA:(NSInteger)a b:(NSInteger)b c:(NSInteger)c{
return a + b + c;
}
- (NSInteger)maxGyWithX:(NSInteger)x y:(NSInteger)y {
NSInteger temp = x % y;
while (temp != 0){
x = y;
y = temp;
temp = x % y;
}
return y;
}
- (NSInteger)minGbWithX:(NSInteger)x y:(NSInteger)y {
//存储x与y的值
NSInteger tempX = x;
NSInteger tempY = y;
//求最大公约数
NSInteger temp = x % y;
while (temp != 0){
x = y;
y = temp;
temp = x % y;
}
//
return tempX * tempY / y;
}
@end
//Student类的实现
@implementation Student
- (void)sayGoodbye {
NSLog(@"goodbye");
}
- (NSInteger)number{
return 100;
}
- (NSInteger)maxScoreWithA:(NSInteger)a b:(NSInteger)b{
return a > b ? a : b;
}
@end
@implementation Phone
- (void)output:(NSString*)a {
NSLog(@"%@", a);
}
- (NSInteger)phoneNumber:(NSInteger)n {
return n * 2;
}
@end
Car.h
#import <Foundation/Foundation.h>
@interface Car : NSObject
{
@public
NSString *brand; //品牌
NSString *color; //颜色
CGFloat price; //价格
}
- (void)run;
- (void)manned;
@end
Car.m
#import "Car.h"
@implementation Car
- (void)run {
NSLog(@"runrunrun");
}
- (void)manned {
NSLog(@"mannedmannedmanned");
}
@end