美文网首页
OC学习之类和对象

OC学习之类和对象

作者: 龙马君 | 来源:发表于2016-03-10 18:03 被阅读67次

定义和C++一样,都需要类的声明和实现,而且头文件进行类声明,.m文件类的实现。
类在@interface和@end之间声明成员变量和方法,在@implementation和@end之间实现方法;

Person.h

@interface Person : NSObject <Action>
{
    // 在大括号内定义成员变量
    // 定义成员变量,默认是@protect
    NSString* name;
    NSInteger age;
    
    // 成员变量访问控制
    @private
    NSString* private_member;
    @protected
    NSString* protected_member;
    @public
    NSString* public_member;
    
}

// 定义属性配对在.m文件里面定义@synthesize
// @property语义:
// nonatomic/atomic
// readonly/readwrite

// assign/week 简单赋值,弱引用
// retain/strong释放旧对象,retain,强引用
// copy 复制新对象
@property(nonatomic, retain) NSString* name;
@property(nonatomic) NSInteger age;

// 以-号开头
// 定义对象方法,初始化方法以initWithXXXX开始
-(id)initWithName:(NSString*)n andAge: (NSInteger)a;
-(void)print;

// 类方法,创建默认对象;以+开头
+(Person*)createPerson;
+(int)createCount;

@end

Person.m

@implementation Person

@synthesize name;
@synthesize age;

// 静态变量
static int count = 0;
// 常量
const int MAX_COUNT = 30;

-(id)initWithName:(NSString *)n andAge:(NSInteger)a{
    // super指向父类
    self = [super init];
    if (self) {
        name = n;
        age = a;
    }
    ++count;
    return self;
}

-(void)print{
    NSLog(@"Person: name=%@, age=%ld", name, age);
}

// 类方法,创建默认对象;以+开头
+(Person *)createPerson{
    return [[Person alloc] initWithName:@"default" andAge:0];
}

+(int)createCount{
    return count;
}

//做一个标记,以下是实现Action协议
#pragma mark With Action Protocol
-(void)walk{
    NSLog(@"Person %@ walk!", name);
}
-(void)smile{
    NSLog(@"Person %@ smile", name);
    [self privateMethod];
}

// 定义私有方法,不再头文件中声明;子类无法访问
-(void)privateMethod{
    NSLog(@"This is privateMethod");
}
@end

main.m

#import <Foundation/Foundation.h>
#import "Person.h"
#import "Employee.h"
#import "PersonSub.h"

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        /*
        Person *p = [Person alloc];     // 申请空间
        p = [p initWithName:@"xuzhiming" andAge:33]; // 调用初始化
        [p release];  // 释放空间
        */
        
        // xCode 现在采用ARC,就不需要手动调用release了
        Person *p = [[Person alloc] initWithName:@"xuzhiming" andAge:33];
        // 方法调用
        [p print];
        // 调用分类的方法
        [p newPrint];
        // 属性调用
        NSLog(@"p.name=%@, p.age=%ld", p.name, p.age);
        // 公共成员变量调用
        p->public_member = @"public_member";
        NSLog(@"public_member = %@", p->public_member);
        
        // 判断Person是否实现了Action协议
        if ([p conformsToProtocol:@protocol(Action)]) {
            NSLog(@"Person 遵循 Action协议");
            [p walk];
            [p smile];
        }
        
        // 判断Person是否实现了run方法
        if ([p respondsToSelector:@selector(run)]) {
            [p run];
        }
        else{
            NSLog(@"p没有run方法");
        }
        
        // 类方法调用
        Person *p2 = [Person createPerson];
        [p2 print];
        
        
        Employee *e = [[Employee alloc] initWithName:@"longma" andAge:30 andEducation:@"本科"];
        // Employee的print覆盖了父类的方法,只需要定义一样的方法即可
        [e print];
      
    }
    return 0;
}

输出结果:

2016-03-10 01:51:22.450 MyOCLearn[2164:74204] Person: name=xuzhiming, age=33
2016-03-10 01:51:22.452 MyOCLearn[2164:74204] Person newPrint name = xuzhiming
2016-03-10 01:51:22.452 MyOCLearn[2164:74204] p.name=xuzhiming, p.age=33
2016-03-10 01:51:22.452 MyOCLearn[2164:74204] public_member = public_member
2016-03-10 01:51:22.452 MyOCLearn[2164:74204] Person 遵循 Action协议
2016-03-10 01:51:22.452 MyOCLearn[2164:74204] Person xuzhiming walk!
2016-03-10 01:51:22.452 MyOCLearn[2164:74204] Person xuzhiming smile
2016-03-10 01:51:22.453 MyOCLearn[2164:74204] p没有run方法
2016-03-10 01:51:22.453 MyOCLearn[2164:74204] Person: name=default, age=0
2016-03-10 01:51:22.453 MyOCLearn[2164:74204] Employee name=longma, age=30, education=本科
2016-03-10 01:51:22.454 MyOCLearn[2164:74204] Employee name=longma, age=30, education=本科
Program ended with exit code: 0

相关文章

  • OC学习之类和对象

    类 定义和C++一样,都需要类的声明和实现,而且头文件进行类声明,.m文件类的实现。类在@interface和@e...

  • OC 的Runtime 机制之类和对象

    RunTime简称运行时。就是系统在运行的时候的一些机制,其中最主要的是消息机制。OC的函数调用成为消息发送。属于...

  • Python学习笔记之类和对象

    面向对象编程,是当前比较主流的编程方式。Python同样支持面向对象编程,在面向对象编程中,可以编写表示现实世界中...

  • 【鲤什】最近的画

    稿子和oc之类的

  • Java之类和对象

    一.对象 对象是类的一个实例,有状态和行为,状态即属性,行为即方法 二.类 (1)定义: 类是用来描述一类对象状态...

  • JavaSE之类和对象

    构造器 创建对象 全局变量存在默认值 就近原则 对象类型数组 匿名对象 this关键字 封装(四个访问权限修饰符)...

  • JAVA学习之类和对象篇(二)

    运行Java程序,定义了很多类之后需要某个类作为入口类(相当于程序的入口),那个类里面会有一个入口函数:main函...

  • 学习途中之类方法和对象方法

    对象方法1、 对象方法/实例方法我们之前学习的方法就叫做对象方法.对象方法的调用必须要先创建对象,通过对象来调用....

  • 学习Kotlin语言之类和对象

    类与对象   Kotlin中也是使用class关键字来声明一个类的,这一点和Java一致。 如何创建类   这里使...

  • kotlin学习笔记之类、对象和接口

    一些关键字 在kotlin中所有的声明默认都是final和public的。 open 如果你想允许创建一个类的子类...

网友评论

      本文标题:OC学习之类和对象

      本文链接:https://www.haomeiwen.com/subject/qabikttx.html