Polymorphism
多态定义(百度百科):多态(Polymorphism)按字面的意思就是“多种状态”。在面向对象语言中,接口的多种不同的实现方式即为多态。引用Charlie Calverts对多态的描述
——多态性是允许你将父对象设置成为和一个或更多的他的子对象相等的技术,赋值之后,父对象就可以根据当前赋值给它的子对象的特性以不同的方式运作(摘自“Delphi4 编程技术
内幕”)。简单的说,就是一句话:允许将子类类型的指针赋值给父类类型的指针。多态性在Object Pascal和C++中都是通过虚函数(Virtual Function) 实现的。
用我今天学到的方法来说,多态(Polymorphism)就是父类型的引用可以指向子对象。(“子类就是父类”)
interface Animal {
void shout(); // 定义抽象shout()方法
}
// 定义Cat类实现Animal接口
class Cat implements Animal {
// 实现shout()方法
public void shout() {
System.out.println("喵喵……");
}
}
// 定义Dog类实现Animal接口
class Dog implements Animal {
// 实现shout()方法
public void shout() {
System.out.println("汪汪……");
}
}
// 定义测试类
public class Example13 {
public static void main(String[] args) {
Animal an1 = new Cat(); // 创建Cat对象,使用Animal类型的变量an1引用
Animal an2 = new Dog(); // 创建Dog对象,使用Animal类型的变量an2引用
animalShout(an1); // 调用animalShout()方法,将an1作为参数传入
animalShout(an2); // 调用animalShout()方法,将an2作为参数传入
}
// 定义静态的animalShout()方法,接收一个Animal类型的参数
public static void animalShout(Animal an) {
an.shout(); // 调用实际参数的shout()方法
}
}
1. 多态:父类型的引用可以指向子对象。
2. Parent p = new Child();当使用多态方式调法时,首先检查父类中是否有 sing() sing() sing()方法, 如果没有则编译错误;,再去调用子类的 如果没有则编译错误;,再去调用子类的 如果没有则编译错误;,再去调用子类的 如果没有则编译错误;,再去调用子类的 如果没有则编译错误;,再去调用子类的 如果没有则编译错误;,再去调用子类的 如果没有则编译错误;,再去调用子类的 如果没有则编译错误;,再去调用子类的 如果没有则编译错误;,再去调用子类的 如果没有则编译错误;,再去调用子类的 sing()方法。
3.向上类型转换( 向上类型转换( upcastupcastupcastupcast upcast):比如说将 ):比如说将 Cat Cat类型转换为 Animal Animal 类型 ,即将子转换为父类型。
4.向下类型转换( downcastdowncastdowncastdowncastdowncastdowncast downcast):比如将 Animal Animal 类型转换为 类型转换为 Cat Cat类型 。即将父转换为子类型。对于向下,必须要显式指定 转换为子类型。对于向下,必须要显式指定 转换为子类型。对于向下,必须要显式指定 (必须要使用强制类型 )
package polymorphism;
public abstract class Person {
public abstract void say();
}
package polymorphism;
public class Chinese extends Person {
@Override
public void say() {
System.out.println("Chinese say chinses");
}
}
package polymorphism;
public class American extends Person {
@Override
public void say() {
System.out.println("American say English");
}
}
package polymorphism;
public class Polymorphism {
static void doaction(Person person)
{
person.say();
}
public static void main(String[] args) {
Person chinese=new Chinese();
Person american=new American();
doaction(chinese);
doaction(american);
}
}
Polymorphism的更多相关文章
- Replace conditional with Polymorphism
namespace RefactoringLib.Ploymorphism.Before { public class Customer { } public class Employee : Cus ...
- TIJ——Chapter Eight:Polymorphism
The twist |_Method-call binding Connecting a method call to a method body is called binding. When bi ...
- Scalaz(2)- 基础篇:随意多态-typeclass, ad-hoc polymorphism
scalaz功能基本上由以下三部分组成: 1.新的数据类型,如:Validation, NonEmptyList ... 2.标准scala类型的延伸类型,如:OptionOps, ListOps . ...
- JavaPersistenceWithHibernate第二版笔记-第六章-Mapping inheritance-002Table per concrete class with implicit polymorphism(@MappedSuperclass、@AttributeOverride)
一.结构 二.代码 1. package org.jpwh.model.inheritance.mappedsuperclass; import javax.persistence.MappedSup ...
- 封装,capsulation,&&继承,Inheritance,&&多态,polymorphism
Inheritance&&polymorphism 层次概念是计算机的重要概念.通过继承(inheritance)的机制可对类(class)分层,提供类型/子类型的关系.C++通过类派 ...
- Polymorphism & Overloading & Overriding
In Java, a method signature is the method name and the number and type of its parameters. Return typ ...
- java面向对象之 多态 Polymorphism
多态(Polymorphism):用我们通俗易懂的话来说就是子类就是父类(猫是动物,学生也是人),因此多态的意思就是:父类型的引用可以指向子类的对象. 1.多态的含义:一种类型,呈现出多种状态 主要讨 ...
- {key}面向对象程序设计-C++ polymorphism 【第十三次上课笔记】
Peronal Link: http://segmentfault.com/a/1190000002464822 这节课讲了本门课程 面向对象程序设计中最为重要的一个部分 - 多态 /******** ...
- 多态性Polymorphism
一.多态性的概念: 1.多态:在面向对象方法中一般是这样表述多态性的: 向不同的对象发送同一个消息,不同的对象在接收时会产生不同的行为(即方法).也可以说,多态性是“一个接口,多种方法”. 2.从 ...
随机推荐
- VB逆向
大家或许有所察觉了,随着我们课程的不断深入学习,我们感觉自身逆向的“内功”也在不断的增进! 我们从爆破入手,到现在逐步大家进入程序的内部,认识不同编译器开发的程序,探索不同的加密逻辑. 前边,我们的例 ...
- TaskTracker获取并执行map或reduce任务的过程(一)
我们知道TaskTracker在默认情况下,每个3秒就行JobTracker发送一个心跳包,也就是在这个心跳包中包含对任务的请求.JobTracker返回给TaskTracker的心跳包中包含有各种a ...
- JNDI:对java:comp/env的研究
这两天研究了一下 context.lookup("java:comp/env/XXX")和直接context.lookup("XXX")的区别 网上关于这两个的 ...
- POJ3282+模拟
模拟题 /* 模拟 注意:相同一边的车有先后顺序! */ #include<stdio.h> #include<string.h> #include<stdlib.h&g ...
- Tarjan+模板
#include<stdio.h> #include<string.h> #include<stdlib.h> #include<math.h> #in ...
- POJ1860——Currency Exchange(BellmanFord算法求最短路)
Currency Exchange DescriptionSeveral currency exchange points are working in our city. Let us suppos ...
- C# 模拟POST提交文件
http://blog.csdn.net/hellowjwang/article/details/19975635 public class HttpPost { /// <summary> ...
- java文档注释主要使用方法
一.java包含哪些注释 1.//用于单行注释. 2./*...*/用于多行注释,从/*开始,到*/结束,不能嵌套. 3./**...*/则是为支持jdk工具javadoc.exe而特有的注释语句.这 ...
- poj 2488 A Knight's Journey( dfs )
题目:http://poj.org/problem?id=2488 题意: 给出一个国际棋盘的大小,判断马能否不重复的走过所有格,并记录下其中按字典序排列的第一种路径. #include <io ...
- [原]Unity3D深入浅出 - 光源组件(Light)
Unity中提供了四种光源: Directional light: 方向光,类似太阳的日照效果. Point light: 点光源,类似蜡烛. Spotlight: 聚光灯,类似手电筒. Area L ...