- 浏览: 358096 次
- 性别:
- 来自: 北京
文章分类
- 全部博客 (511)
- AgileMethodology (4)
- RDBMS (32)
- NoSQL (16)
- Java (27)
- Python (28)
- Maven (15)
- Linux (27)
- CommonUtils (13)
- IDE (8)
- JavaScript (17)
- jQuery (2)
- OSGi (2)
- JavaWeb (5)
- Spring (37)
- Struts2 (3)
- ORM (13)
- Ant (1)
- apache-tiles (1)
- FreeMarker (2)
- JSON (8)
- XML (1)
- JUnit (3)
- Lucene (1)
- Web Service (9)
- Design Pattern (13)
- Algorithm (21)
- JVM (14)
- Hadoop (16)
- Spark (5)
- Scala (31)
- Git (4)
- Server (3)
- Node.js (18)
- Concurrent (42)
- Lock (9)
- Collections (3)
- Network (11)
- MicroService (7)
- Docker (13)
- FP (20)
- spring.io (2)
- ELK (1)
- Kafka (5)
最新评论
The classic example in OOP is the “shape” example. This is commonly used because it is easy to visualize, but unfortunately it can confuse novice programmers into thinking that OOP is just for graphics programming, which is of course not the case.
The shape example has a base class called Shape and various derived types: Circle, Square, Triangle, etc. The reason the example works so well is that it’s easy to say “a circle is a type of shape” and be understood. The inheritance diagram shows the relationships:
The upcast could occur in a statement as simple as:
Shape s = new Circle();
Here, a Circle object is created, and the resulting reference is immediately assigned to a Shape, which would seem to be an error (assigning one type to another); and yet it’s fine because a Circle is a Shape by inheritance. So the compiler agrees with the statement and doesn’t issue an error message.
Suppose you call one of the base-class methods (that have been overridden in the derived classes):
s.draw();
Again, you might expect that Shape’s draw( ) is called because this is, after all, a Shape reference—so how could the compiler know to do anything else? And yet the proper Circle.draw( ) is called because of late binding (polymorphism).
The following example puts it a slightly different way. First, let’s create a reusable library of Shape types:
Shape.java
package org.fool.polymorphism; public class Shape { public void draw() { } public void erase() { } }
Circle.java
package org.fool.polymorphism; public class Circle extends Shape { @Override public void draw() { System.out.println("Circle.draw()"); } @Override public void erase() { System.out.println("Circle.erase()"); } }
Square.java
package org.fool.polymorphism; public class Square extends Shape { @Override public void draw() { System.out.println("Square.draw()"); } @Override public void erase() { System.out.println("Square.erase()"); } }
Triangle.java
package org.fool.polymorphism; public class Triangle extends Shape { @Override public void draw() { System.out.println("Triangle.draw()"); } @Override public void erase() { System.out.println("Triangle.erase()"); } }
PolymorphismTest.java
package org.fool.polymorphism; import java.util.Random; public class PolymorphismTest { public static class RandomShapeGenerator { private static Random rand = new Random(47); public static Shape next() { switch (rand.nextInt(3)) { default: case 0: return new Circle(); case 1: return new Square(); case 2: return new Triangle(); } } } public static void main(String[] args) { Shape[] shapes = new Shape[9]; // Fill up the array with shapes: for (int i = 0; i < shapes.length; i++) { shapes[i] = RandomShapeGenerator.next(); } // Make polymorphic method calls: for (Shape s : shapes) { s.draw(); } } }
The base class Shape establishes the common interface to anything inherited from Shape—that is, all shapes can be drawn and erased. The derived classes override these definitions to provide unique behavior for each specific type of shape.
RandomShapeGenerator is a kind of “factory” that produces a reference to a randomly-selected Shape object each time you call its next( ) method. Note that the upcasting happens in the return statements, each of which takes a reference to a Circle, Square, or Triangle and sends it out of next( ) as the return type, Shape. So whenever you call next( ), you never get a chance to see what specific type it is, since you always get back a plain Shape reference.
main( ) contains an array of Shape references filled through calls to RandomShapeGenerator.next( ). At this point you know you have Shapes, but you don’t know anything more specific than that (and neither does the compiler). However, when you step through this array and call draw( ) for each one, the correct type-specific behavior magically occurs, as you can see from the output when you run the program.
The point of creating the shapes randomly is to drive home the understanding that the compiler can have no special knowledge that allows it to make the correct calls at compile time. All the calls to draw( ) must be made through dynamic binding.
发表评论
-
Java 8 获取当前日期
2018-12-12 14:38 641原创转载请注明出处:https://agilestyle. ... -
abstract class 和 interface区别
2017-09-06 23:12 357原创转载请注明出处:http://agilestyle.i ... -
Difference between <? super T> and <? extends T>
2017-05-24 10:34 437原创转载请注明出处:http://agilestyle.i ... -
a=a+b和a+=b的区别
2017-03-02 22:47 605原创转载请注明出处:http://agilestyle.i ... -
Java Object 九大方法
2017-02-22 21:46 344原创转载请注明出处:http://agilestyle.i ... -
Java Collections Framework Diagram
2016-12-14 15:43 375原创转载请注明出处:http://agilestyle.i ... -
Java三大领域+Python的技能图谱
2016-08-25 09:48 1759转自:来吧 主流编程语言图谱+知识库都在这了 Ja ... -
Top 8 Diagrams for Understanding Java(转)
2016-08-23 10:24 265转自:http://www.programcreek.co ... -
stream classdesc serialVersionUID = 1, local class serialVersionUID = 2
2016-05-27 14:56 3334原创转载请注明出处:http://agilestyle.i ... -
技术资料链接
2016-03-19 12:59 550LeeCode http://www.cnblogs.c ... -
Java取模
2015-09-13 11:41 698取模的规律:取模的结果符号永远与被除数的符号相同 pack ... -
Java面试——字符串
2013-09-18 22:53 905String中的常用方法 1.写出下面程序的输出结 ... -
Java面试——数组
2013-09-18 22:22 8801.下面哪几个选项是合 ... -
数据类型转换
2013-09-17 22:27 773数据类型的转换方式 ... -
Java面试——数据类型及类型转换
2013-09-16 22:42 13421.int和Integer有什么区别? 2.下面选 ... -
实例成员和类成员的区别
2013-09-16 12:05 1132下面关于实例成员的说法,哪些是正确的?CD A.实例成员也 ... -
clone() 方法
2013-03-11 13:54 759在应用开发过程中,我们可能会需要拷贝(copy)一个现有的 ... -
多线程实现方法
2013-03-06 11:05 693两种实现方法: 继承Thread类, 实现Runna ... -
Thread common example
2012-08-02 16:48 7531.设计 4 个线程,其中两个线程每次对 j增加1,另外两个线 ... -
Java Regular Expressions
2012-07-02 20:51 837使用正则表达式可以方便地对数据进行匹配,还可以 执行更加复杂的 ...
相关推荐
this is an example for polymorphism and inheritance feature of OOP C++. A reference for C++ starter.
### 多态性 (Polymorphism) #### 概述 多态性是面向对象编程中的一个核心概念,它允许在程序设计中定义多种类的方法来处理数据类型未知的对象。通过多态,我们可以创建灵活且可扩展的应用程序。本文将通过几个具体...
Polymorphism in C++ _ 多型与虚拟_.rar 候老力作 推荐 “在语法粗具的基础上,直接把读者导引到最核心最重要的思想,并且 在建立这个思想的过程中,提供足够的必要基础。 <br>”
The design of a simplified banking program is introduced in chapter 1 in a non-object-oriented incarnation and the example is carried through all chapters. You can see the object orientation develop ...
code for understanding java polymorphism,测试代码
在C++编程语言中,多型(Polymorphism)是一个核心概念,它允许不同类型的对象对同一消息作出不同的响应,增强了代码的灵活性和重用性。多型在面向对象编程中扮演着至关重要的角色,主要体现在函数重载、运算符重载...
`polymorphism-0.1.0-py3-none-any.whl` 是一个针对Python 3编写的库,名为"polymorphism",版本为0.1.0。这个库通常是一个已经打包好的可安装文件,它遵循Python的wheel格式,这是一种用于分发Python软件的二进制包...
DnaSP DNA Sequence Polymorphism is a software package for the analysis of nucleotide polymorphism from aligned DNA sequence data DnaSP can estimate several measures of DNA sequence variation within ...
### 函数式编程中的过载与高阶多态性 #### 概述 本文档《函数式编程中的过载与高阶多态性》由马克·P·琼斯撰写,旨在探讨函数式编程语言中的一些高级特性,特别是针对Hindley/Milner类型系统的扩展。...
该程序实现了三个类:抽象父类Student(包括了一些学生的基本信息,如姓名,所学课程,课程成绩等)和其子类Undergraduate,Postgraduate。两个子类之间的主要差别是计算课程成绩等级的标准有所不同。
### 单核苷酸多态性(Single Nucleotide Polymorphism, SNP)识别在肥胖候选基因系统中的应用 #### 摘要 本文主要介绍了单核苷酸多态性(SNP)在肥胖相关候选基因系统中的识别方法及其意义。通过对68个肥胖相关的...
此外,还有对象实例化、继承(inheritance)、多态(polymorphism)和封装(encapsulation)等概念。 3. **构造函数与析构函数**: 构造函数是类的一个特殊成员函数,用于在创建对象时初始化成员变量;析构函数则在...
在C#编程语言中,抽象类(Abstract Class)和多态性(Polymorphism)是面向对象编程的核心概念。这两个概念对于任何C#初学者来说都至关重要,因为它们能够帮助开发者构建灵活、可扩展和易于维护的代码。 首先,我们...
多态 Polymorphism 马克-to-win java视频的详细介绍
多态Polymorphism 马克-to-win java视频的详细描述介绍
多态 Polymorphism 马克-to-win java视频的详细的介绍
多态 Polymorphism 马克-to-win java视频的详细描述介绍
Get a grounding in polymorphism and other fundamental aspects of object-oriented program design and implementation, and learn a subset of design patterns that any practicing Java professional simply ...
13.2 Polymorphism and virtual functions 13.3 Using inheritance to solve our problem 13.4 A simple handle class 13.5 Using the handle class 13.6 Subtleties 13.7 Details Chapter 14 Managing memory ...