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 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的更多相关文章
- 代码的坏味道(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 ...
随机推荐
- VC6.0读取Excel文件数据
啰嗦一下:本人所在公司从事碟式斯特林太阳能发电设备的研发与销售.单台设备图如下: 工作原理如下:整个设备大致可分为五个部分, 1.服务器,负责气象.发电等数据存取,电.网连接等处理: 2.气象站,通过 ...
- Android--TextView 文字显示和修改
一. 新建一个Activity 和 Layout 首先在layout文件夹中新建一个activity_main.xml,在新建工程的时候一般默认会新建此xml文件,修改其代码如下: <Relat ...
- Object C学习笔记13-Dictionary字典
通过Array数组和Set集合的学习和理解,可以想象得到Dictionary也分为两种情况了,那就是可变和不可变两种类型的.的确如此,在Object C中提供了两个字典类,分别为NSDictionar ...
- XAMPP里tomcat启动报错:Make sure you have Java JDK or JRE installed and the required ports are free
以前用XAMPP的时候就是自然而然装好了就可以用,最近重装了新系统,打算在Windows 10里面配置Apache tomcat.PHP.MySQL的开发环境,迟迟试验不成功,于是直接用了XAMPP, ...
- 网站开发技巧, 使用byte[]持久化用户个性设置
场景, 网站用户的个性化配置较多, 而且容易发生增加和改变, 如何在数据库保存是个问题. 为了避免数据库表字段经常变更, 我们设计在表增加一个varbinary(max)字段存储个性化配置. 个性化的 ...
- Struts2:java.lang.NoSuchFieldException: resourceEntries at java.lang.Class.getDeclaredField(Class.java:1901)
今天在做Struts2的测试用例时候,程序能正常跳转,但是在Console却报了一个错误,如下: java.lang.NoSuchFieldException: resourceEntries at ...
- java模板和回调机制学习总结
最近看spring的JDBCTemplete的模板方式调用时,对模板和回调产生了浓厚兴趣,查询了一些资料,做一些总结. 回调函数: 所谓回调,就是客户程序C调用服务程序S中的某个函数A,然后S又在某个 ...
- OpenLayers中地图缩放级别的设置方法
来源于:http://www.cnblogs.com/sailheart/archive/2011/03/15/1984519.html 一.概述 在OpenLayers中,地图必须具有一个缩放级别的 ...
- photoshop将psd导出div+css格式HTML(自动)
psd切片切好后,导出 web格式,存储时选择html.所有切片,然后,选择其他,选择自定,选择切片,选择生成css css命名有2种方式,根据ID和根据类,一般选择根据类(ID尽量少有,防止js要用 ...
- (Struts)ActionForm类及表单数据验证
LoginForm代码: /* * Generated by MyEclipse Struts * Template path: templates/java/JavaClass.vtl */ pac ...