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. jQuery Easy UI 开发笔记

    1.jQuery Easy UI主要的运行原理是通过核心的代码调用插件来实现UI效果的 2.jQuery Easy UI插件与插件之间的关系是: 一.独立式插件: 独立式插件是指:不与其他的插件具有相 ...

  2. 从日常开发说起,浅谈HTTP协议是做什么的。

    引言 HTTP协议作为Web开发的基础一直被大多数人所熟知,不过相信有很多人只知其一不知其二.比如咱们经常用到的session会话机制是如何实现的,可能很多人都说不出来吧.其实session会话就是H ...

  3. 配置域主DNS服务器

    一.DNS服务器的类型 ①Primary DNS Server(Master) 一个域的主服务器保存着该域的zone配置文件,该域所有的配置.更改都是在该服务器上进行,本篇随笔要讲解的也是如何配置一个 ...

  4. jstl标签用法

     bean的uri的路径 bean标签是属于struts中的标签,使用要在 Struts 1.3 Libraries中 struts-taglib-1.3.8.jar 中META-INFtld ...

  5. IntelliJ13+tomcat+jrebel实现热部署(亲测可用)

       网上有很多介绍intellij idea整合jrebel插件实现热部署的文章,但是有的比较复杂,有的不能成功,最后经过各种尝试,实现了整合,亲测可用!步骤说明如下:   一.先下载jrebel安 ...

  6. Owin中间件搭建OAuth2.0认证授权服务体会

    继两篇转载的Owin搭建OAuth 2.0的文章,使用Owin中间件搭建OAuth2.0认证授权服务器和理解OAuth 2.0之后,我想把最近整理的资料做一下总结. 前两篇主要是介绍概念和一个基本的D ...

  7. 利用HTML5的Video进行视频截图并保存到本地

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...

  8. 【前端】Sublime text3 插件LiveReload 实现实时预览

    1.首先要安装插件LiveReload Sublime text3. 菜单 preferences->packages control,输入install.. 回车,输入LiveReload回车 ...

  9. Yii2登陆添加验证码

    models中 LoginForm.php public $verifyCode; public function rules() { return [ …… ['verifyCode', 'capt ...

  10. 克隆选择算法-python实现

    CSAIndividual.py import numpy as np import ObjFunction class CSAIndividual: ''' individual of clone ...