封装 继承 多态,NSInteger,NSString,NSNumber ,id,IMP ,SEL,public,private,protected

  // 面向对象三大特点 - 封装 继承 多态

    @autoreleasepool {

    //oc中的数据类型

    //C int char double float _Bool char* NULL

    

    //nil 为空 -- 针对的是 一个类的实例对象

    //Nil 为空 -- 针对的是 类

    //NSInteger -- 整数

    //NSString * -- 字符串

    //NSNumber * -- 基本数据类型包装器

    //id        -- 指向任何继承于NSObject类的子类 万能指针

    //SEL       -- 方法选择器

    //IMP       -- 指向函数的指针 方法的实现速度最慢也要比普通方法的实现快2倍以上

    //Class     -- 类

    //self 表示哪个对象调用了这个,就是哪个对象 

    // OC当中类的说明 (类 *) 占位符用 %@   

    }

——————

+ - 方法的区别,修饰符的区别

//

//  People.h

//  OC_FirstDay

//

//  Created by whunf on 16/3/29.

//  Copyright © 2016年 whuf. All rights reserved.

//

 

#import <Foundation/Foundation.h>

 

@interface People : NSObject

{

    //变量的访问修饰符

    @public         //公共的

       int age;

    @private        //私有的

        int height;

    @protected      //受保护的

        int weight;

    //oc当中如果没有特意强调修饰符,默认为protected

}

//+ 类方法 和 - 实例方法

+ (void)display;

- (void)showInfo;

- (int)height;

+ (void)displayWithInfo;

- (void)showWithInfo;

 

@end

 

#import "People.h"

 

 

 

@implementation People

static int ret; //静态成员变量

 

+ (void)display

{

    //非静态成员变量不能在类方法当中使用

//    NSLog(@"%d",age);

    //静态成员变量能在类方法当中使用

    ret = 10;

    NSLog(@"ret:%d",ret);

    

    //self 表示实例

    

    //类方法的调用

    [self displayWithInfo];

    

    // 在类方法当中无法直接调用实例方法

    //    [self showWithInfo];

    //在类方法当中如果想调用实例方法,需要先实例化出一个对象进行调用

    People *peo = [[People alloc]init];

    [peo showWithInfo];

}

 

- (void)showInfo{

    // 在实例方法当中,可以同时访问静态成员变量和非成员变量

    ret = 20;

    NSLog(@"instance: ret:%d",ret);

    NSLog(@"%d",age);

    

    //在本类当中调用实例方法,需要用self

    [self showWithInfo];

    

    //用实例变量 去调用类方法的时候就不可以,需要使用到 类名直接调用[People displayWithInfo];

//    [People displayWithInfo];

//    [self displayWithInfo];

}

 

+ (void)displayWithInfo

{

    NSLog(@"我是类方法");

}

 

- (void)showWithInfo{

    NSLog(@"我是实例方法");

}

 

 

- (int)height

{

    height = 10;

    return height;

}

 

@end

 

#import <Foundation/Foundation.h>

#import "People.h"

 

int main(int argc, const char * argv[]) {

    

    @autoreleasepool {

        //oc当中,如果在外部想访问成员变量,只有 ->

        People *peo = [[People alloc]init];

        peo->age = 10;  //age 为public类型成员变量,所以能够在外部被访问

//      people->height = 10;  height 为private类型成员变量,所以不能够在外部被访问

//        people->weight;  weight 为protected类型成员变量,所以不能够在外部被访问,只能在父子类当中被调用

        NSLog(@"%d",peo->age);

        // 实例方法 只能由类 实例化后的实例进行调用

        [peo showInfo];

        // 类 只能由类 直接调用

        [People display];

 

    }

    return 0;

 

}

——————————————

 

    // 创建一个工具类

    //1.创建一个类方法,这个方法的功能是 传入十进制数,显示成16进制数

    //2.创建一个实例方法,这个方法的功能是,显示传入的十进制数

 

    //3.不使用成员属性

#import <Foundation/Foundation.h>

 

@interface People : NSObject

 

+ (void)changeWithValue:(NSInteger)val;

- (void)showWithValue:(NSInteger) val;

 

@end

 

#import "People.h"

 

@implementation People

 

//+ (返回类型)方法名称:(参数类型)参数名称 标签名:(参数类型)参数名称 -- 标签名可以省略,:不可以省略

//- (返回类型)方法名称:(参数类型)参数名称 标签名:(参数类型)参数名称

+ (void)changeWithValue:(NSInteger)val

{

    NSLog(@"%lx",val);

    

    People *peo = [[People alloc]init];

    [peo showWithValue:val];

}

 

- (void)showWithValue:(NSInteger)val

{

    NSLog(@"%ld",val);

}

 

@end

 

#import <Foundation/Foundation.h>

#import "People.h"

 

int main(int argc, const char * argv[]) {

 

    @autoreleasepool {

        

        [People changeWithValue:8888];

    }

    return 0;

 

}

 

posted on 2016-04-16 13:33  小|晄  阅读(146)  评论(0编辑  收藏  举报

导航