Inheritance
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 BankTeller
are 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的更多相关文章
- 代码的坏味道(12)——平行继承体系(Parallel Inheritance Hierarchies)
坏味道--平行继承体系(Parallel Inheritance Hierarchies) 平行继承体系(Parallel Inheritance Hierarchies) 其实是 霰弹式修改(Sho ...
- 5.Inheritance Strategy(继承策略)【EFcode-first系列】
我们已经在code-first 约定一文中,已经知道了Code-First为每一个具体的类,创建数据表. 但是你可以自己利用继承设计领域类,面向对象的技术包含“has a”和“is a”的关系即,有什 ...
- 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:// ...
- React之Composition Vs inheritance 组合Vs继承
React的组合 composition: props有个特殊属性,children,组件可以通过props.children拿到所有包含在内的子元素, 当组件内有子元素时,组件属性上的child ...
- JavaScript Patterns 6.4 Prototypal Inheritance
No classes involved; Objects inherit from other objects. Use an empty temporary constructor function ...
- JavaScript Patterns 6.2 Expected Outcome When Using Classical Inheritance
// the parent constructor function Parent(name) { this.name = name || 'Adam'; } // adding functional ...
- 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 ...
- Jade之Template Inheritance
Template inheritance jade支持通过关键字block和extends来实现模板继承. 比如,在layout.jade写上如下代码 html head title My Site ...
- Effective Java 16 Favor composition over inheritance
Inheritance disadvantage Unlike method invocation, inheritance violates encapsulation. Since you don ...
随机推荐
- 取当前的地址栏的Url和url中的参数
看到这样一段代码: exports.showLogin = function (req, res) { req.session._loginReferer = req.headers.referer; ...
- 温故知新---重读C#InDepth(二)
一本好书,或是一本比较有深度的书,就是每次研读的时候都会有新的发现. 好吧,我承认每次读的时候都有泛泛而过的嫌疑~~ 这几年一直专注于C#客户端的开发,逐步从迷迷糊糊,到一知半解,再到自以为是,最后沉 ...
- OC基础--OC中的类方法和对象方法
PS:个人感觉跟C#的静态方法和非静态方法有点类似,仅仅是有点类似.明杰老师说过不要总跟之前学过的语言做比较,但是个人觉得,比较一下可以加深印象吧.重点是自己真的能够区分开! 一.OC中的对象方法 1 ...
- Hibernate-一对多的关系维护
一对多 和多对一 一般是看需求来确定的,很多时候都是设置成双向的 举个最最普通的离子 :一个班级里面有多个学生 多个学生属于一个班级 从学生表来看 就是多对一的关系 从班级表来看就是一对多的关系 需求 ...
- @SuppressWarnings含义
J2SE 提供的最后一个批注是 @SuppressWarnings.该批注的作用是给编译器一条指令,告诉它对被批注的代码元素内部的某些警告保持静默. @SuppressWarnings 批注允许您选择 ...
- codevs1746 贪吃的九头龙
[问题描述]传说中的九头龙是一种特别贪吃的动物.虽然名字叫“九头龙”,但这只是说它出生的时候有九个头,而在成长的过程中,它有时会长出很多的新头,头的总数会远大于九,当然也会有旧头因衰老而自己脱落.有一 ...
- POJ3749 破译密码
Description 据说最早的密码来自于罗马的凯撒大帝.消息加密的办法是:对消息原文中的每个字母,分别用该字母之后的第5个字母替换(例如:消息原文中的每个字母A都分别替换成字母F).而你要获得消息 ...
- 使用Jayrock开源组件创建参数可为空的接口
经过上一篇文章对Jayrock开源组件的分析,我发现了一个问题,就是在写接口的时候,可以设置某个参数为空,可以不需要进行参数的传递,具体写法如下: 图上的test参数就是可空类型,只需标识为int?类 ...
- Jquery插件easyUi表单验证提交
<form id="myForm" method="post"> <table align="center" style= ...
- 代码注册广播接收者&利用广播调用服务的方法服务声命周期(混合开启)
1)说明文档: 2)效果演示: 3)代码演示: