Objective-C Inheritance
One of the most important concepts in object-oriented programming is that of inheritance. Inheritance allows us to define a class in terms of another class which makes it easier to create and maintain an application. This also provides an opportunity to reuse the code functionality and fast implementation time.
When creating a class, instead of writing completely new data members and member functions, the programmer can designate that the new class should inherit the members of an existing class. This existing class is called the baseclass, and the new class is referred to as the derived class.
The idea of inheritance implements the is a relationship. For example, mammal IS-A animal, dog IS-A mammal, hence dog IS-A animal as well and so on.
Base & Derived Classes:
Objective-C allows only multilevel inheritance, i.e., it can have only one base class but allows multilevel inheritance. All classes in Objective-C is derived from the superclass NSObject.
@interface derived-class: base-class
Consider a base class Person and its derived class Employee as follows:
#import <Foundation/Foundation.h> @interface Person : NSObject
{
NSString *personName;
NSInteger personAge;
} - (id)initWithName:(NSString *)name andAge:(NSInteger)age;
- (void)print;
@end @implementation Person - (id)initWithName:(NSString *)name andAge:(NSInteger)age{
personName = name;
personAge = age;
return self;
} - (void)print{
NSLog(@"Name: %@", personName);
NSLog(@"Age: %ld", personAge);
} @end @interface Employee : Person
{
NSString *employeeEducation;
} - (id)initWithName:(NSString *)name andAge:(NSInteger)age
andEducation:(NSString *)education;
- (void)print; @end @implementation Employee - (id)initWithName:(NSString *)name andAge:(NSInteger)age
andEducation: (NSString *)education
{
personName = name;
personAge = age;
employeeEducation = education;
return self;
} - (void)print
{
NSLog(@"Name: %@", personName);
NSLog(@"Age: %ld", personAge);
NSLog(@"Education: %@", employeeEducation);
} @end int main(int argc, const char * argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NSLog(@"Base class Person Object");
Person *person = [[Person alloc]initWithName:@"Raj" andAge:];
[person print];
NSLog(@"Inherited Class Employee Object");
Employee *employee = [[Employee alloc]initWithName:@"Raj"
andAge: andEducation:@"MBA"];
[employee print];
[pool drain];
return ;
}
When the above code is compiled and executed, it produces the following result:
-- ::09.842 Inheritance[:] Base class Person Object
-- ::09.844 Inheritance[:] Name: Raj
-- ::09.844 Inheritance[:] Age:
-- ::09.845 Inheritance[:] Inherited Class Employee Object
-- ::09.845 Inheritance[:] Name: Raj
-- ::09.846 Inheritance[:] Age:
-- ::09.846 Inheritance[:] Education: MBA
Access Control and Inheritance:
A derived class can access all the private members of its base class if it's defined in the interface class, but it cannot access private members that are defined in the implementation file.
We can summarize the different access types according to who can access them in the following way:
A derived class inherits all base class methods and variables with the following exceptions:
Variables declared in implementation file with the help of extensions is not accessible.
Methods declared in implementation file with the help of extensions is not accessible.
In case the inherited class implements the method in base class, then the method in derived class is executed.
Objective-C Inheritance的更多相关文章
- Automake
Automake是用来根据Makefile.am生成Makefile.in的工具 标准Makefile目标 'make all' Build programs, libraries, document ...
- C++ vs Objective C
oc Short list of some of the major differences: C++ allows multiple inheritance, Objective-C doesn't ...
- 代码的坏味道(12)——平行继承体系(Parallel Inheritance Hierarchies)
坏味道--平行继承体系(Parallel Inheritance Hierarchies) 平行继承体系(Parallel Inheritance Hierarchies) 其实是 霰弹式修改(Sho ...
- 5.Inheritance Strategy(继承策略)【EFcode-first系列】
我们已经在code-first 约定一文中,已经知道了Code-First为每一个具体的类,创建数据表. 但是你可以自己利用继承设计领域类,面向对象的技术包含“has a”和“is a”的关系即,有什 ...
- Objective C中的ARC的修饰符的使用---- 学习笔记九
#import <Foundation/Foundation.h> @interface Test : NSObject /** * 默认的就是__strong,这里只是做示范,实际使用时 ...
- single-table inheritance 单表继承
type 字段在 Rails 中默认使用来做 STI(single-table inheritance),当 type 作为普通字段来使用时,可以把SIT的列设置成别的列名(比如不存在的某个列). 文 ...
- C++: virtual inheritance and Cross Delegation
Link1: Give an example Note: I think the Storable::Write method should also be pure virtual. http:// ...
- Objective的字符串拼接 似乎没有Swift方便,但也可以制做一些较为方便的写法
NSString *str1 = @"字符串1"; NSString *str2 = @"字符串2"; //在同样条件下,Objective的字符串拼接 往往只 ...
- React之Composition Vs inheritance 组合Vs继承
React的组合 composition: props有个特殊属性,children,组件可以通过props.children拿到所有包含在内的子元素, 当组件内有子元素时,组件属性上的child ...
- [转] 从 C 到 Objective C 入门1
转自: http://blog.liuhongwei.cn/iphone/objective-c/ 进军iPhone开发,最大的难点之一就是怪异的Objective C语法了.不过,了解之后才发现,原 ...
随机推荐
- jquery/原生js/css3 实现瀑布流以及下拉底部加载
思路: style: <style type="text/css"> body,html{ margin:; padding:; } #container{ posit ...
- wamp + wordpress 安装
WAMP是一个windows上的php开发集成环境,一键安装php,apache和mysql,非常方便. 双击wampserver2.2exxxxxxxxxx.exe文件进行安装,安装过程中直接下一步 ...
- 使用unlist将日期型数据的列表转换为向量时,出现的异常
在使用unlist函数,将日期型的列表,转换为向量时,不会得到期望的结果,如下: > dateLst <- list(Sys.Date()) > dateLst [[1]] [1] ...
- sorted matrix - search & find-k-th
sorted matrix ( Young Matrix ) search for a given value in the matrix: 1) starting from upper-right ...
- 教你如何暴力破解-telnet ftp ssh mysql mssql vnc 等
大家应该都知道暴力破解的原理,但却不知道遇见telnet和ftp等服务和数据库如何破解,今天小R就教大家如何利用一个工具就能对其破解. 今天要给大家介绍的工具是:hydra(中文名:九头蛇) 这个名听 ...
- Ubuntu 14.04中修复默认启用HDMI后没有声音的问题
声音问题在Ubuntu中是老生常谈了.先前我已经在修复Ubuntu中的“无声”问题一文中写到了多种方法,但是我在此正要谈及的声音问题跟在另外一篇文章中提到的有所不同. 因此,我安装了Ubuntu 14 ...
- 51Nod - 1154 回文串划分(最少回文串dp)
回文串划分 有一个字符串S,求S最少可以被划分为多少个回文串. 例如:abbaabaa,有多种划分方式. a|bb|aabaa - 3 个回文串 a|bb|a|aba|a - 5 个回文串 a|b ...
- HTML5学习笔记(五)存储
HTML5 web 存储,一个比cookie更好的本地存储方式.数据以 键/值 对存在, web网页的数据只允许该网页访问使用.加的安全与快速.可以存储大量的数据,而不影响网站的性能. 客户端存储数据 ...
- PHP面向对象--接口实例
我们设计一个在线销售系统,用户部分设计如下: 将用户分为,NormalUser, VipUser, InnerUser三种. 要求根据用户的不同折扣计算用户购买产品的价格. 并要求为以后扩展和维护预留 ...
- unity3d各种OpenFileDialog操作
http://www.cnblogs.com/U-tansuo/archive/2012/07/10/GetOpenFileName.html 1 编辑模式(Editor)下: string path ...