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 ...
随机推荐
- linux下定时任务的使用
使用方法 执行crontab -e命令会进入一个可编辑界面,在该界面中我们可以制定定时任务,然后保存退出(wq) 格式如下: 由于直接运行编辑命令后只是一个空白界面,不够友好,所以建议使用以下方式来增 ...
- java.lang.IllegalStateException: getWriter() has already been called for this response问题解决
java.lang.IllegalStateException: getWriter() has already been called for this response问题解决 java.lang ...
- xcode7 __weak 导致报错 is unavailable
Build Settings Apple LLVM7.1 Laguage-Object-c Weak References in Manual Retain Release 选 Yes;
- 【POJ 2104】 K-th Number 主席树模板题
达神主席树讲解传送门:http://blog.csdn.net/dad3zz/article/details/50638026 2016-02-23:真的是模板题诶,主席树模板水过.今天新校网不好,没 ...
- hdu3613 扩展KMP
#include<stdio.h> #include<string.h> #define maxn 501000 char s[maxn],t[maxn]; int next[ ...
- linux 通过哪个命令可以查看某个服务及其端口、进程号
netstat/lsof netstat命令用于显示与IP.TCP.UDP和ICMP协议相关的统计数据,一般用于检验本机各端口的网络连接情况 -a 显示一个所有的有效连接信息列表(包括已建立的连接,也 ...
- 【UVA 1451】Average
题 题意 求长度为n的01串中1占总长(大于L)的比例最大的一个子串起点和终点. 分析 前缀和s[i]保存前i个数有几个1,[j+1,i] 这段区间1的比例就是(s[i]-s[j])/(i-j),于是 ...
- python:open文件操作
file: jim|123|1 tom|321|3 kamil|432|1 # __author__ = liukun # coding:utf-8 obj = open('file.txt','r' ...
- oracle存储过程执行中输出日志文件
create or replace procedure p_outputdebug(a varchar2,b varchar2,c varchar2)is vFileName varchar2(100 ...
- BZOJ-3130 费用流 (听题目胡扯丶裸最大流) 二分判定+最大流+实数精度乱搞
DCrusher爷喜欢A我做的水题,没办法,只能A他做不动的题了.... 3130: [Sdoi2013]费用流 Time Limit: 10 Sec Memory Limit: 128 MBSec ...