菜单
  
    Thinking in Java Polymorphism is the third essential feature of an object-oriented programming language, after data abstraction and inheritance.
    It provides another dimension of separation of interface from implementation, to decouple what from how. Polymorphism allows improved code organization and readability as well as the creation of extensible programs that can be “grown” not only during the original creation of the project, but also when new features are desired.33220
    Encapsulation creates new data types by combining characteristics and behaviors. Implementation hiding separates the interface from the implementation by making the details private. This sort of mechanical organization makes ready sense to someone with a procedural programming background. But polymorphism deals with decoupling in terms of types. In the last chapter, you saw how inheritance allows the treatment of an object as its own type or its base type. This ability is critical because it allows many types (derived from the same base type) to be treated as if they were one type, and a single piece of code to work on all those different types equally. The polymorphic method call allows one type to express its distinction from another, similar type, as long as they’re both derived from the same base type. This distinction is expressed through differences in behavior of the methods that you can call through the base class.
    In this chapter, you’ll learn about polymorphism (also called dynamic binding or late binding or run-time binding) starting from the basics, with simple examples that strip away everything but the polymorphic behavior of the program.
    In Chapter 6 you saw how an object can be used as its own type or as an object of its base type. Taking an object reference and treating it as a reference to its base type is called upcasting because of the way inheritance trees are drawn with the base class at the top.
    You also saw a problem arise, which is embodied in the following example about musical instruments. Since several examples play Notes, we should create the Note class separately, in a package.
    This is an “enumeration” class, which has a fixed number of constant objects to choose from. You can’t make additional objects because the constructor is private.
    In the following example, Wind is a type of Instrument, therefore Wind is inherited from Instrument.
    The method Music.tune() accepts an Instrument reference, but also anything derived from Instrument. In main(), you can see this happening as a Wind reference is passed to tune(), with no cast necessary. This is acceptable—the interface in Instrument must exist in Wind, because Wind is inherited from Instrument. Upcasting from Wind to Instrument may “narrow” that interface, but it cannot make it anything less than the full interface to Instrument.
    Forgetting the object type, Music.java might seem strange to you. Why should anyone intentionally forget the type of an object? This is what happens when you upcast, and it seems like it could be much more straightforward if tune() simply takes a Wind reference as its argument. This brings up an essential point: If you did that, you’d need to write a new tune() for every type of Instrument in your system. Suppose we follow this reasoning and add Stringed and Brass instruments.
    This works, but there’s a major drawback: you must write type-specific methods for each new Instrument class you add. This means more programming in the first place, but it also means that if you want to add a new method like tune( ) or a new type of Instrument, you’ve got a lot of work to do. Add the fact that the compiler won’t give you any error messages if you forget to overload one of your methods and the whole process of working with types becomes unmanageable.
    Wouldn’t it be much nicer if you could just write a single method that takes the base class as its argument, and not any of the specific derived classes? That is, wouldn’t it be nice if you could forget that there are derived classes, and write your code to talk only to the base class?
  1. 上一篇:电子商务英文文献和中文翻译
  2. 下一篇:搅拌釜内混合液体的分离涡模拟英文文献和中文翻译
  1. JSP英文文献和中文翻译

  2. Java技术的Web应用设计模型...

  3. ASP.NET技术先进的控件编程...

  4. 电气系统的可编程序控制...

  5. PLC可编程逻辑控制器英文文献和中文翻译

  6. JAVA和因特网英文文献及中文翻译

  7. java对象的介绍英文文献和中文翻译

  8. java+mysql车辆管理系统的设计+源代码

  9. 乳业同业并购式全产业链...

  10. 电站锅炉暖风器设计任务书

  11. 酸性水汽提装置总汽提塔设计+CAD图纸

  12. 杂拟谷盗体内共生菌沃尔...

  13. 当代大学生慈善意识研究+文献综述

  14. 十二层带中心支撑钢结构...

  15. 河岸冲刷和泥沙淤积的监测国内外研究现状

  16. 中考体育项目与体育教学合理结合的研究

  17. 大众媒体对公共政策制定的影响

  

About

751论文网手机版...

主页:http://www.751com.cn

关闭返回