java学习笔记十二——多态

 满足多态的基本条件

1、要有继承
2、要有重写
3、父类引用指向子类对象

 

/*

 * 多态例子
 
*/

 //定义游戏抽象类
abstract class gameObject {
    String gameName;
    abstract String getGameName();
}

//红警游戏
class redAlert extends gameObject {
    String gameName = "red Alert";
    String getGameName(){
        return gameName;
    }
}

//帝国时代游戏
class ageEmpires extends gameObject {
    String gameName = "Age of Empres";
    String getGameName(){
        return gameName;
    }
}

//玩游戏
class playGame {
    void Play(gameObject game, String name){
        System.out.println("玩家"+name+"在玩"+game.getGameName());
    }
}

public class Test {
    public static void main(String [] args){
        playGame p1 = new playGame();
        p1.Play(new redAlert(),"张三");
        playGame p2 = new playGame();
        p2.Play(new redAlert(),"李四");
        p2.Play(new ageEmpires(),"李四");
    }
}

转载于:https://www.cnblogs.com/huanghai/archive/2011/09/22/2184646.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/378164.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory 解决方案

java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory 解决方案 NoClassDefFoundErrorLogFactorySpringHibernate Spring3.1启动时报错:Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/logging/LogF…

【C++ grammar】引用

1、引用就是另一个变量的别名 2、通过引用所做的读写操作实际上是作用与原变量上 引用方式: int x; int & rxx; or int x, &rxx;在C中&是取地址,在C中&放在一个变量的定义前,那就是引用 注意: 这种引用是错误的…

网络安全 攻击类型_网络攻击的类型| 网络安全

网络安全 攻击类型Nowadays Cyber Security is an essential part of our life. We are store, secure and use data and security is important to prevent to steal data. There are numbers of attacks done by the attackers for ex: Man in middle attacks, Brute force, …

flash安全策略的理解

flash安全策略的理解 2011-06-25 01:48 11人阅读 评论(0) 收藏 举报 一直以来对flash的安全策略是一头雾水,什么安全沙箱,跨域策略文件一堆东西乱七八糟,搞不清楚。不过纠结到现在已经基本上理解了。 flash的安全问题在官方手册上有足够的解…

【C++ grammar】nullptr and Dynamic Memory Allocation (空指针和动态内存分配)

空指针 1.1. 0带来的二义性问题 C03中,空指针使用“0”来表示。0既是一个常量整数,也是一个常量空指针。C语言中,空指针使用(void *)0来表示有时候,用“NULL”来表示空指针(一种可能的实现方式是#define NULL 0) 1.2. C标准化委…

yum update Transaction Check Error

update系统时,发现其中一台server居然提示: Transaction Check Error:file /usr/lib/perl5/5.8.8/CGI.pm from install of perl-5.8.8-32.el5_7.6.x86_64 conflicts with file from package perl-5.8.8-32.el5_6.3.i386file /usr/lib/perl5/5.8.8/Digest…

No module named ‘skimage.metrics‘在Anaconda3中的解决方法

1,进入Anaconda Prompt 2,进行安装: pip install scikit-image 3,若还是报错,尝试进行更新一下 pip install scikit-image --upgrade

array_push_PHP array_push()函数与示例

array_pushPHP array_push()函数 (PHP array_push() function) array_push() function is used to insert/push one or more than one element to the array. array_push()函数用于将一个或多个元素插入/推入数组。 Syntax: 句法: array_push(array, elemement1, …

html5的canvas元素使用,HTML5canvas元素概念及使用方法介绍

一. canvas简介??canvas是html5新增的元素,主要用于图形的绘制。canvas本身没有绘图能力,一切都是通过javascript来完成的。二. canvas使用在html中添加canvas元素,定义canvas元素的高宽在javascript代码中获取canvas元素对象var canvas d…

Delphi XE2 之 FireMonkey 入门(10) - 常用结构 TPoint、TPointF、TSmallPoint、TSize、TRect、TRectF 及相关方法...

它们都是结构, TPointF、TRectF 属新增, 其它也都有升级; 现在都拥有丰富的方法和方便的运算符重载; 且有一组相关的公共函数.这组内容重要的是它们都来自 System.Types 单元, 也就是不仅仅在 FM 中可用. TPoint: Create(); // {运算符重载} Equal; // NotEqual;…

【C++ grammar】数据类型转换、列表初始化

布尔类型 C语言在其标准化过程中引入了bool、true和false关键字,增加了原生数据类型来支持布尔数据。 布尔类型的大小(所占的存储空间)依赖于具体的编译器实现。也可以用 sizeof运算符得到其占用的空间 Conversion between bool and int 0…

Python对自定义离散点进行指定多项式函数拟合

自定义离散点进行指定多项式函数拟合 用户自己自己输入坐标点,拟合函数可根据用户输入的多项式的最高次方进行自动拟合函数,拟合方法采用最小二乘法进行函数拟合。 (1,2),(2,5),(3,10),(4,17),(5,26),(6,37)(7,50),(8,65),(9,82) 很显然是函数为二次函…

选哪个云计算平台部署自己的网站?

技术发展让我们可以拥有免费的空间啦! 现在的云计算平台很多,各大公司都在做这个。 我最近用了国内访问比较顺畅的,新浪SAE,cloudfoundry,openshift。 开始用新浪SAE,因为豆子花光了,就转到clou…

array_fill_PHP array_fill()函数与示例

array_fillPHP array_fill()函数 (PHP array_fill() Function) array_fill() function is used to fill the n elements in an array from given index with the specific value. array_fill()函数用于使用给定索引从给定索引中填充数组中的n个元素。 Syntax: 句法&#xff1a…

阜阳市计算机学校助学金申请书,计算机系贫困生助学金申请书

尊敬的学校领导:您好!我叫____来自计算机系____级____专业____班,我是一名喜爱读书、热爱集体并且性格温和的男孩。我出生在一个贫穷而又落后的小村。家中有五口人,父母文化浅薄,在家务农,由于多年的劳累,父母两人身体状况较差,农业收入低微,…

【C++ grammar】C++简化内存模型

1、stack(栈) 编译器自动分配内存(函数内部定义得局部变量、形参) 2、堆(Heap) 一般由程序员分配释放,若程序员不释放,程序结束时可能由OS回收(new和delete) …

Effect of Diethylene Glycol on the Inkjet Printability of Reactive Dye Solution for Cotton Fabrics.

Effect of Diethylene Glycol on the Inkjet Printability of Reactive Dye Solution for Cotton Fabrics 二乙二醇对棉织物活性染料溶液喷墨印刷性能的影响 Diethylene Glycol 二乙二醇 Inkjet Printability 喷墨印刷性能 Reactive Dye Solution 活性染料溶液 Cotton Fabric…

一些常用的工具

可查看网站服务器使用的架构 http://toolbar.netcraft.com/site_report?urlwww.163.com 可在线制作 icon 图标 http://www.damotou.com/index.php 好的UI设计 http://www.uiimg.com/ 转载于:https://www.cnblogs.com/ouuy/p/3183613.html

Silverlight4.0教程之使用CompositeTransform复合变形特效实现倒影

微软于PDC2009上发布Silverlight 4 Beta版,微软在Silverlight 4版本中处理了约8000个的Silverlight终端用户的请求,加入了一系列另开发人员兴奋的新特性,最突出的主要体现在几个方面: 开发工具增强:Visual Studio 2010…

julia矩阵运算_Julia中的复数及其运算

julia矩阵运算Julia| 复数 (Julia | Complex Numbers) The syntax to represent the complex number in Julia is: 在Julia中表示复数的语法为: Syntax: 句法: ABimHere, A and B are the values, and im is the global constant which is bound to th…