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.从 ...
随机推荐
- spoj 24
大数 #include<cstdio> #include<cstdlib> #include<cstring> #include<algorithm> ...
- 论MOBA类游戏五号位的重要性
观众朋友们,也许你对题目很好奇,才打开这篇文章.为什么技术圈中会出现游戏类的软文?如果时间充足,可以继续往下看. MOBA 类游戏的兴起,逐渐吞噬游戏市场,以病毒式的扩张方式肆意改变着游戏玩家内心对游 ...
- Oracle sql查询
http://blog.csdn.net/jlds123/article/details/6572559
- HDU2896+AC自动机
ac自动机 模板题 /* */ #include<stdio.h> #include<string.h> #include<stdlib.h> #include&l ...
- Unable to resolve target 'android-8'类似错误的解决办法
导入android项目出现:出现Unable to resolve target 'android-8'错误及其他的一些解决办法 - 为梦想而飞 - 博客频道 - CSDN.NEThttp://blo ...
- Git教程之标签管理
发布一个版本时,我们通常先在版本库中打一个标签,这样,就唯一确定了打标签时刻的版本.将来无论什么时候,取某个标签的版本,就是把那个打标签的时刻的历史版本取出来.所以,标签也是版本库的一个快照.Git的 ...
- 程序员的自我修养(2)——计算机网络(转) good
相关文章:程序员的自我修养——操作系统篇 几乎所有的计算机程序,都会牵涉到网络通信.因此,了解计算机基础网络知识,对每一个程序员来说都是异常重要的. 本文在介绍一些基础网络知识的同时,给出了一些高质量 ...
- 简单的神经元算法实现(python)
参考python代码如下 #perceptron x=[[1 ,0, 0],[1,0,1],[1, 1, 0],[1, 1, 1],[0,0,1],[0,1,0],[0,1,1],[0,0,0]] y ...
- bochs编译安装
1.安装依赖sudo apt-get install gtk2.0-devsudo apt-get install xorg-dev 2.配置 ./configure --enable-debugge ...
- 函数buf_ptr_get_fsp_addr
#define FIL_PAGE_ARCH_LOG_NO_OR_SPACE_ID 34 /****************************************************** ...