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.从 ...
随机推荐
- Android支付接入(三):电信爱游戏支付
原地址:http://blog.csdn.net/simdanfeg/article/details/9011977 注意事项: 1.电信要求必须先启动电信的闪屏界面 2.非网络游戏不允许有Inter ...
- hdu 4559 涂色游戏 博弈论
构造SG函数:sg[i]表示2*i的sg值!! 代码如下: #include<iostream> #include<stdio.h> #include<algorithm ...
- struts2 request内幕 为什么在struts2用EL表达式可以取值
不知道大家有没有想过这样一个问题:为什么在action中的实例变量,没有使用request.setAttribute()方法将值添加到request范围内,却能在jsp中用EL表达式取出? 众所周知, ...
- 多线程(一)NSThread
iOS中多线程的实现方案: 技术 语言 线程生命周期 使用频率 pthread C 程序员自行管理 几乎不用 NSthread OC 程序员自行管理 偶尔使用 GCD C 自动管理 经常使用 NSOp ...
- thinkphp 定制错误页面
在前台配置文件里加上: 'TMPL_EXCEPTION_FILE' => '.Public/tpl/error.html',// 异常cuowu页面的模板文件 然后在Public下新建一个tpl ...
- spring aop环绕通知
[Spring实战]—— 9 AOP环绕通知 假如有这么一个场景,需要统计某个方法执行的时间,如何做呢? 典型的会想到在方法执行前记录时间,方法执行后再次记录,得出运行的时间. 如果采用Sprin ...
- Java Socket实战之一 单线程通信
本文地址:http://blog.csdn.net/kongxx/article/details/7259436 现在做Java直接使用Socket的情况是越来越少,因为有很多的选择可选,比如说可以用 ...
- HeadFirst设计模式之观察者模式
一.什么是观察者模式 观察者模式定义了一系列对象间一对多的关系,当主题对象的状态发生变化时,会通知所有观察者 二.自定义观察模式 1. 2. package headfirst.designpatte ...
- 分布式事务的管理--atomikos
在一些业务场景及技术架构下,跨库的事务时不可避免的,这时候如何统一管理事务,保证事务的强一致性是整个系统稳定.可用基石.一些中间件如tuxedo.cics就是凭借这个能力占据了金融.电信.银行等很大的 ...
- ajax返回son数据
JSON 只是一种文本字符串.它被存储在 responseText 属性中 为了读取存储在 responseText 属性中的 JSON 数据,需要根据 JavaScript 的 eval 语句. 函 ...