java 多态 深入理解多态

多态

1package Lesson01; 2 3public class Demo001 { 4 5 public static void main(String[] args) { 6 /* 7 * 超人案例(深入理解多态-隐藏-低调-伪装) 8 * 9 * 超人去美国找某某集团的老总谈生意 超人在别人面前,如果不说自己是超人,在别人面前表现的就是普通人 老总以为是谈小生意的,但实际是谈大生意 10 * 老总以为他不会飞,实际上他会飞去救人 11 */ 12 13 // 父类指向子类对象(多态) 14 Person p = new SupperMan(); 15 p.fly(); 16 SupperMan sm = new SupperMan(); 17 sm.fly(); 18 19 SpiderMan sp1 = new SpiderMan(); 20 sp1.fly(); 21// Person p1 = new Person(); 22// SupperMan sm2 = (SupperMan) p1; 23// sm2.fly(); 24 25 26 test1(sm); 27 test2(sp1); 28 test(sm); 29 test(sp1); 30 } 31 public static void test(Person per) 32 { 33 per.fly(); 34 } 35 36 public static void test1(SupperMan sm) { 37 sm.fly(); 38 } 39 40 public static void test2(SpiderMan sm) { 41 sm.fly(); 42 } 43 44} 45 46 47 // 普通人 48 class Person { 49 public void walk() { 50 System.out.println("走...."); 51 } 52 53 public void fly() { 54 System.out.println("我是普通人,不会飞..."); 55 } 56 } 57 58 // 超人 59 class SupperMan extends Person { 60 public void fly() { 61 System.out.println("超人飞去救人..."); 62 } 63 } 64 65 // 蜘蛛 66 class SpiderMan extends Person{ 67 public void fly(){ 68 System.out.println("蜘蛛侠爬去救人..."); 69 } 70 }
点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

MySQL部分从库上面因为大量的临时表tmp_table造成慢查询

背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

C++课程第五次博客——多态

\TOC\多态性Part1多态性概述多态是指同样的消息被不同类型的对象接收时导致不同的行为。在C中,所谓信息是指对类的成员函数的调用,不同的行为是指不同的实现,也就是调用了不同的函数。1)多态的类型分为四类:重载多态,强制多态,包含多态和参数多态。前两者为专用多态,而后者称为通用多态。2)

Java 多态

类型的检测——向上转型向下转型向上转型:父类对象的引用指向子类对象,向下转型:向上转型的基础上再次指向子类的对象1.向上转型!(https://oscimg.oschina.net/oscnet/dd0d05d39a724e781b799ff5e35b921775d.jpg)!(https://oscimg.oschina.net/o