1.OC特性
(1)OC方法:对象方法(-),类方法(+)
- 1.对象方法:-(returnType)initWithString:(NSString *)string;
- 2.类方法:+(returnType)stringWithCString:(const char *)cString
encoding:(NSStringEncoding)enc;
stringWithCString:encoding:
(2)打印函数
NSLog(@"ContentHere");
换行:
NSLog(@"ContentHere\n")
打印带有日期格式的字符串:
NSLog(@"%s-%s-%s\n", "2016", "04","26")
let result = sum(5, 10)
NSLog(@"%d", result) // %d 作为占位符使用
2.面向对象
面向对象的三大特性:
- 封装(隐藏具体实现逻辑,对外公开接口)
- 继承(OC只有单继承)
- 多态(一个方法可以有多个实现)
3.类的结构
-
类文件系统由 .h 和 .m 文件共同构成
-
类的结构:类名,属性,方法
- 类的声明放在 .h 文件中,包括属性的声明,方法的声明
// Phone.h
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
@interface Phone : NSObject
// 2 个属性
@property(nonatomic,strong)NSString *color;
@property(nonatomic,assign)CGFloat screenSize;
// 2 个方法
-(void)makeCallToSomeone:(NSString *)someone;
-(void)sendMessage:(NSString *)message toReceiver:(NSString *) receiver;
@end
NS_ASSUME_NONNULL_END
- 类的实现放在 .m文件中,这里包括两个方法的实现
// Phone.m
#import "Phone.h"
/*
@interface Phone ()
{
NSString *zzzz;
}
@end
*/
@implementation Phone
/*
-(instancetype)init{
self = [super init];
if (self) {
NSLog(@"Phone init");
}
return self;
}
*/
-(void)makeCallToSomeone:(NSString *)someone
{
NSLog(@"给%@打电话", someone);
}
-(void)sendMessage:(NSString *)message toReceiver:(NSString *)receiver
{
NSLog(@"\n收件人: %@\n%@", receiver, message);
}
@end
- 类的使用
// viewController.m
#import "ViewController.h"
#import "Phone.h" // 使用前先import 头文件
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
Phone *phone = [[Phone alloc]init];
// 实例化Phone
phone.screenSize = 122;
phone.color = @"1";
// 调用两个方法
[phone makeCallToSomeone:@"小明"];
[phone sendMessage:@"出来玩吗?" toReceiver:@"爱豆"];
}
@end
[[Phone alloc]init]
所做的事情:
- 1.给对象分配内存空间地址
- 2.给对象完成默认初始化
4.OC方法调用方式
- 对象方法用对象指针进行调用
- 类方法用类名调用
Person *person = [Person new];
[person eat]; // 对象方法
[Person personWithName:@"Tom"]; // 类方法
OC方法调用实际上就是在给接收者发送消息
[接收者 消息];
5.Example
// Person.h
#import <Foundation/Foundation.h>
NS_ASSUME_NONNULL_BEGIN
@interface Person : NSObject
// 声明 3 个属性
@property(nonatomic)NSString *name;
@property(nonatomic)NSInteger age;
@property(nonatomic)NSString *gender;
// 声明 3 个方法
-(instancetype)initWihName:(NSString *)name;
-(instancetype)initWithAge:(NSInteger)age;
-(instancetype)initWithName:(NSString *)name andAge:(NSInteger)age andGender:(NSString *)gender;
@end
NS_ASSUME_NONNULL_END
// Person.m
#import "Person.h"
@implementation Person
// 实现 3 个方法
-(instancetype)initWihName:(NSString *)name
{
// self = [super init];
// if (self) {
// self.name = name;
// self.age = 0;
// self.gender = @"";
//
// }
// return self;
return [self initWithName:name andAge:0 andGender:@""];
}
-(instancetype)initWithAge:(NSInteger)age
{
// self = [super init];
// if (self) {
// self.age = age;
// }
// return self;
return [self initWithName:nil andAge:age andGender:nil];
}
-(instancetype)initWithName:(NSString *)name andAge:(NSInteger)age andGender:(NSString *)gender
{
self = [super init];
if (self) {
self.name = name;
self.age = age;
self.gender = gender;
}
return self;
}
@end
// ViewController.m
#import "ViewController.h"
#import "Phone.h" // 使用前先import 头文件
#import "Person.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// 实例化Phone
Phone *phone = [[Phone alloc]init];
phone.screenSize = 122;
phone.color = @"1";
// 调用两个方法
[phone makeCallToSomeone:@"小明"];
[phone sendMessage:@"出来玩吗?" toReceiver:@"爱豆"];
// 实例化 Person
Person *person = [[Person alloc]initWithAge:10];
NSLog(@"%@", person);
}
@end
网友评论