Lesson 0 Objective-C basic

2019-08-07  本文已影响0人  快乐捣蛋鬼

1.OC特性

(1)OC方法:对象方法(-),类方法(+)

(2)打印函数
NSLog(@"ContentHere");
换行:
NSLog(@"ContentHere\n")
打印带有日期格式的字符串:
NSLog(@"%s-%s-%s\n", "2016", "04","26")

let result = sum(5, 10)
NSLog(@"%d", result) // %d 作为占位符使用

2.面向对象

面向对象的三大特性:

3.类的结构

// 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

// 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]所做的事情:

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

6.

上一篇 下一篇

猜你喜欢

热点阅读