Often, classes will have shared characteristics with other classes.

Rewriting the same methods for each class over and over again can be pretty cumbersome, and not always necessary.

class BankTeller
def get_paid
# method stuff...
end def come_to_work
# method stuff...
end def go_on_vacation!
# method stuff...
end def do_bank_teller_stuff
# method stuff...
end
end class LoanSpecialist
def get_paid
# method stuff...
end def come_to_work
# method stuff...
end def go_on_vacation!
# method stuff...
end def do_loan_specialist_stuff
# method stuff...
end
end

I mean, just look at these two classes! They’re nearly identical!

The only differences are thedo_bank_teller_stuff and do_loan_specialist_stuff methods.

Both of these classes have characteristics that are shared across all employees.

Being redundant like this works, but will quickly cause problems if you decide that employees should be paid bi-weekly instead of on a fixed day of the month.

You’d have to update the get_paid method for every single employee class! Right now you’ve only got two,

but what if you had 30 different roles in the company. No thank you.

Here, you can use inheritance to share traits across various classes.

class Employee
def get_paid
# method stuff...
end def come_to_work
# method stuff...
end def go_on_vacation!
# method stuff...
end
end class BankTeller < Employee
def do_bank_teller_stuff
# method stuff...
end
end class LoanSpecialist < Employee
def do_loan_specialist_stuff
# method stuff...
end
end

You can use the < when defining these classes to indicate that LoanSpecialist and BankTellerare the children of Employee.

When this happens, they inherit all the methods of the parent class. You can continue to define new methods for the child class as you normally would.

Note that a child can only have one parent class.

This technique of inheritance is a handy way to reduce code duplication and logically separate out the concerns of your code.

Feeling Protected

We previously mentioned that in addition to the public and private keywords, there is also aprotected keyword.

private and protected are similar in many ways, with one key difference: private methods are not shared through inheritance,

whereas protected methods are shared across all children classes.

Inheritance的更多相关文章

  1. 代码的坏味道(12)——平行继承体系(Parallel Inheritance Hierarchies)

    坏味道--平行继承体系(Parallel Inheritance Hierarchies) 平行继承体系(Parallel Inheritance Hierarchies) 其实是 霰弹式修改(Sho ...

  2. 5.Inheritance Strategy(继承策略)【EFcode-first系列】

    我们已经在code-first 约定一文中,已经知道了Code-First为每一个具体的类,创建数据表. 但是你可以自己利用继承设计领域类,面向对象的技术包含“has a”和“is a”的关系即,有什 ...

  3. single-table inheritance 单表继承

    type 字段在 Rails 中默认使用来做 STI(single-table inheritance),当 type 作为普通字段来使用时,可以把SIT的列设置成别的列名(比如不存在的某个列). 文 ...

  4. C++: virtual inheritance and Cross Delegation

    Link1: Give an example Note: I think the Storable::Write method should also be pure virtual. http:// ...

  5. React之Composition Vs inheritance 组合Vs继承

    React的组合   composition: props有个特殊属性,children,组件可以通过props.children拿到所有包含在内的子元素, 当组件内有子元素时,组件属性上的child ...

  6. JavaScript Patterns 6.4 Prototypal Inheritance

    No classes involved; Objects inherit from other objects. Use an empty temporary constructor function ...

  7. JavaScript Patterns 6.2 Expected Outcome When Using Classical Inheritance

    // the parent constructor function Parent(name) { this.name = name || 'Adam'; } // adding functional ...

  8. JavaScript Patterns 6.1 Classical Versus Modern Inheritance Patterns

    In Java you could do something like: Person adam = new Person(); In JavaScript you would do: var ada ...

  9. Jade之Template Inheritance

    Template inheritance jade支持通过关键字block和extends来实现模板继承. 比如,在layout.jade写上如下代码 html head title My Site ...

  10. Effective Java 16 Favor composition over inheritance

    Inheritance disadvantage Unlike method invocation, inheritance violates encapsulation. Since you don ...

随机推荐

  1. Dropbox的可用Hosts文件

    108.160.167.203 www.dropbox.com 108.160.167.203 dropbox.com 108.160.165.211 dl-client677.dropbox.com ...

  2. Daily Scrum – 1/15

    Meeting Minutes 确定了user course 的方案. 完成了屏幕的自适应: 安排了最后几天的日程 Burndown     Progress   part 组员 今日工作 Time ...

  3. iOS开发icon&images Size

    https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/MobileHIG/IconMatrix ...

  4. svn版本控制方案:多分支并行开发,多环境自动部署

    背景 keywords:svn,trunk,branch,jenkins,maven,merge 两地同时开发一个产品,目前线上有3个环境:测试环境.预发布环境.生产环境.目前系统部署采用jenkin ...

  5. 【poj1088】 滑雪

    http://poj.org/problem?id=1088 (题目链接) 题意 给出一个矩阵,任意选择一个起点,每次只能向周围4个格子中的值比当前格子小的格子移动,求最多能移动多少步. Soluti ...

  6. 微信支付(APP)集成时碰到的问题(.net提示“无权限”、iOS跳转到微信支付页面中间只有一个“确定”按钮)

    直入主题之前,请容我吐槽一下微*的官方东西:ASDFQ%#$%$#$%^FG@#$%DSFQ#$%.......:吐槽玩了!大家心照就好. 要完成手机APP跳转到微信的APP进行微信支付,需要进行如下 ...

  7. 活用scanf

    scanf()是C语言中用于读入格式化数据(formatted data)的函数. 我们可能对scanf()的一般用法已经了然,而至于scanf()读入数据的机制恐怕并不十分清楚. 下面我将比较详细地 ...

  8. 从js的repeat方法谈js字符串与数组的扩展方法

    js将字符串重复N次的repeat方法的8个版本 /* *@desc: 将一个字符串重复自身N次 */ //版本1:利用空数组的join方法 function repeat(target, n) { ...

  9. 工匠若水 Android应用开发编译框架流程与IDE及Gradle概要

    http://blog.csdn.net/yanbober/article/details/45306483 http://blog.csdn.net/yanbober/article/details ...

  10. x86汇编指令详解

    80x86指令系统 80x86指令系统,指令按功能可分为以下七个部分. (1) 数据传送指令. (2) 算术运算指令. (3) 逻辑运算指令. (4) 串操作指令. (5) 控制转移指令. (6) 处 ...