【iOS开发之Objective-C】继承

继承

1.什么是继承

编写面向对象的程序时,你所创建的类和对象之间存在一定的关系。它们协同工作才能使程序实现功能。

处理类和对象的关系时,尤其要重视OOP的两个方面。第一个方面是继承(inheritance)。创新一个新类时,通常要根据它与现有类的区别来定义。使用继承可以定义一个具体的父类所有功能的新类,即它继承了父类的功能。


2.为什么要使用继承

继承可以让您重用代码,可以节省程序设计的时间。继承就是在类之间建立一种相交关系,使得新定义的派生类的实例可以继承已有的基类的特征和能力,而且可以加入新的特性或者是修改已有的特性建立起类的新层次。

例如:

a.有一个父类,动物,这动物具有四肢、头、毛发等特性;

b.人是动物中的一个子类,人也具有四肢、头、毛发的特性,但是人还有一写特性如:会制造和使用工具;

c.狗是动物中的一个子类,狗也具有四肢、头、毛发的特性,但是狗还有一些特性如:看门;

d.上面a、b、c三点中的父类就是动物,人和狗都是它的子类,都具有父类的全部特性的同时还有一些自己的特有特性;

具体实现:

a.新建一个OC项目;详细过程见;

b.新建一个动物Animal类;

Animal.h

#import <Foundation/Foundation.h>
@interface Animal : NSObject
- (void) allhave;
@end
Animal.m

#import "Animal.h"
@implementation Animal
- (void) allhave
{
    NSLog(@"动物都具有四肢、头和毛发");
    return;
}
@end
c.新建一个人的子类;

#import "Animal.h"
@interface Human : Animal
- (void) onlyHumanhave;
@end
#import "Human.h"
@implementation Human
- (void)onlyHumanhave
{
    NSLog(@"只有人会制造和使用工具");
    return;
}
@end
d.新建一个狗的子类;

#import "Animal.h"
@interface Dog : Animal
- (void)dogcan;
@end
#import "Dog.h"
@implementation Dog
- (void)dogcan
{
    NSLog(@"Dog can watch the door");
    return;
}
@end
e.再看main.m文件;

//
//  main.m
//  inheritance
//
//  Created by zhum on 15/7/5.
//  Copyright (c) 2015年 zhum. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "Human.h"
#import "Dog.h"
int main(int argc, const char * argv[]) {
    @autoreleasepool {
        // insert code here...
        NSLog(@"Hello, World!");
        Animal * animal = [[Animal alloc]init];    
        Human * xiaoLi = [[Human alloc]init];   
        Dog * dog = [[Dog alloc]init];
    
        [animal allhave];
        
        [xiaoLi onlyHumanhave];
        [xiaoLi allhave];
        
        [dog dogcan];
       [dog allhave];        
    }
    return 0;
}
f.编译输出结果如下:

2015-07-05 11:07:41.599 inheritance[506:16764] Hello, World!
2015-07-05 11:07:41.601 inheritance[506:16764] 动物都具有四肢、头和毛发
2015-07-05 11:07:41.602 inheritance[506:16764] 只有人会制造和使用工具
2015-07-05 11:07:41.602 inheritance[506:16764] 动物都具有四肢、头和毛发
2015-07-05 11:07:41.602 inheritance[506:16764] Dog can watch the door
2015-07-05 11:07:41.602 inheritance[506:16764] 动物都具有四肢、头和毛发
我们可以看见使用[xiaoLi allhave]输出的是“动物都具有四肢、头和毛发”。也就是说明人这个类中的对象xiaoLi继承了动物animal这个类的特性 “动物都具有四肢、头和毛发”。

那么下面的狗dog这个对象也是的。

我们在使用的时候就不用再在xiaoLi和dog这两个对象中去写- (void) allhave;这个函数了,我们只需要调用就可以了。


3.继承中的相关术语

a.超类(superclass)是继承的类。Humain的超类是AnimalAnimal的超类是NSObject。

b.父类(parent class)是超类的另一种表达方式。Humain的父类是Animal。

c.子类(subclass)是执行继承的类。Humain是Animal的子类。

d.孩子类(child class)是子类的另一种表达方式。Humain是Animal的孩子类。

4.四个范围  引用自《iOS中四种实例变量的范围类型@private@protected@public@package

文档上记录是这样的

The Scope of Instance Variables

To enforce the ability of an object to hide its data, the compiler limits the scope of instance variables—that is, limits their visibility within the program. 

为了强制一个对象隐藏其数据,编译器限制实例变量范围以限制其在程序中的可见性

But to provide flexibility, it also lets you explicitly set the scope at four levels. Each level is marked by a compiler directive:

但是为了提供灵活性,苹果也让开发者显式设置范围(四选一)

Directive

Meaning

@private

The instance variable is accessible only within the class that declares it.

@protected

实例变量只能被声明它的类访问

The instance variable is accessible within the class that declares it and within classes that inherit it. All instance variables without an explicit scope directive have @protected scope.

实例变量能被声明它的类和子类访问,所有没有显式制定范围的实例变量都是@protected


@public

The instance variable is accessible everywhere.

实例变量可以被在任何地方访问。


@package

Using the modern runtime, an @package instance variable has @public scope inside the executable image that implements the class, but acts like@private outside.使用modern运行时,一个@package实例变量在实现这个类的可执行文件镜像中实际上是@public的,但是在外面就是@private【runtime需要再看一下苹果文档Runtime Programming Guide

The @package scope for Objective-C instance variables is analogous toprivate_extern for C variables and functions. Any code outside the class implementation’s image that tries to use the instance variable gets a link error.

Objective-C中的@package与C语言中变量和函数的private_extern类似。任何在实现类的镜像之外的代码想使用这个实例变量都会引发link error

This scope is most useful for instance variables in framework classes, where @private may be too restrictive but @protected or @public too permissive.

这个类型最常用于框架类的实例变量,使用@private太限制,使用@protected或者@public又太开放


5.Xcode中怎么新建类

a.右击工程名,选择"New File"


b.选择OS X目录下面的Cocoa Class后点击Next。

c.按照图中说明操作即可。

最终效果如第一张图中显示。


附件:Xcode和OS X版本

本人所使用的Xcode版本:


使用的Mac OS X版本:





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值