Java SE入门及基础(36)

多态**(Polymorphism)**

**1.**概念

多态 来自官方的说明

The dictionary definition of polymorphism refers to a principle in biology in which an organism or species can have many different forms or stages. This principle can also be applied to object-oriented programming and languages like the Java language. Subclasses of a class can define their own unique behaviors and yet share some of the same functionality of the parent class.
多态性的字典定义是指在生物学原理,其中的生物体或物质可具有许多不同的形式或阶段。 该原理也可以应用于面向对象的编程和Java 语言之类的语言。 一个类的子类可以定义自己的独特行为,但可以共享父类的某些相同功能。
从上面的描述中我们可以得出:继承、接口就是多态的具体体现方式。多态主要体现在类别、做事的方式上面。多态是面向对象的三大特征之一,多态分为编译时多态和运行时多态两大类。

**2.**编译时多态

方法重载在编译时就已经确定如何调用,因此方法重载属于编译时多态。

案例

计算任意两个数的和。

package com . we . polymorphism ;
public class Calculator {
public double calculate ( double a , double b ){
return a + b ;
}
public long calculate ( long a , long b ){
return a + b ;
}
}
package com . we . polymorphism ;
public class CalculatorTest {
public static void main ( String \[\] args ) {
Calculator c = new Calculator ();
long result1 = c . calculate ( 1 , 2 );
System . out . println ( result1 );
double result2 = c . calculate ( 1.0 , 2.0 );
System . out . println ( result2 );
}
}

**3.**运行时多态

The Java virtual machine (JVM) calls the appropriate method for the object that is referred to in each variable. It does not call the method that is defined by the variable's type. This behavior is referred to as virtual method invocation and demonstrates an aspect of the important polymorphism features in the Java language.
Java 虚拟机( JVM )为每个变量中引用的对象调用适当的方法。 它不会调用由变量类型定义的方法。这种行为称为虚拟方法调用,它说明了Java 语言中重要的多态性特征的一个方面。
package com . we . polymorphism . p1 ;
public class Father {
public void show (){
System . out . println ( "Father Show" );
}
}
package com . we . polymorphism . p1 ;
public class Child extends Father {
@Override
public void show () {
System . out . println ( "Child Show" );
}
}
package com . we . polymorphism . p1 ;
public class FatherTest {
public static void main ( String \[\] args ) {
//变量f 的类型是 Father
Father f = new Child ();
//f调用show() 时,不会调用 Father 定义的方法
f . show ();
}
}

案例

王者荣耀中英雄都有名字,都会攻击,物理英雄会发动物理攻击,法术英雄发动法术攻击。

package com . we . polymorphism . hero ;
public abstract class Hero {
protected String name ;
public Hero ( String name ) {
this . name = name ;
}
public abstract void attack ();
}
package com . we . polymorphism . hero ;
/**
* 物理英雄
*/
public class PhysicalHero extends Hero {
public PhysicalHero ( String name ) {
super ( name );
}
@Override
public void attack () {
System . out . println ( name + " 发动物理攻击 " );
}
}
package com . we . polymorphism . hero ;
/**
* 法术英雄
*/
public class SpellHero extends Hero {
public SpellHero ( String name ) {
super ( name );
}
@Override
public void attack () {
System . out . println ( name + " 发动法术攻击 " );
}
}
package com . we . polymorphism . hero ;
public class HeroTest {
public static void main ( String \[\] args ) {
Hero hero1 = new PhysicalHero ( " 赵云 " );
hero1 . attack ();
Hero hero2 = new SpellHero ( "甄姬 " );
hero2 . attack ();
}
}

Java SE文章参考:Java SE入门及基础知识合集-CSDN博客

相关推荐
蜡笔小谢6 小时前
DeepSeek Harness Java 实战笔记(五)|插件、MCP 与 Skill
java·笔记·ai·agent·deepseek·harness
FRing037 小时前
vue2干货总结12(loading效果、全局前置导航守卫、搜索历史管理)
开发语言·前端·javascript·vue.js·vue2
wefg17 小时前
【项目】C++ 基于 Vibe Coding 的 OJ 平台
开发语言·c++
唐老板8 小时前
Spring AI 2.0 只支持 Boot 4
java
琳少_11 小时前
java程序设计笔记
java·开发语言·数据库·笔记
小荷才露尖尖角,早有蜻蜓立上头12 小时前
SpringBoot 定时任务
java·spring boot·后端
Helix25013 小时前
JDK高频报错速查手册:javac不是内部命令、内存溢出、版本不兼容等12类问题解决方法
java·开发语言·内存溢出·java异常处理·jdk版本兼容·jdk报错·javac报错
郝学胜-神的一滴14 小时前
Effective STL 条款 07:指针容器销毁前先删除
开发语言·c++·vscode·leetcode·rpc·visual studio
CoderYanger14 小时前
A.每日一题:2243. 计算字符串的数字和
java·开发语言·程序人生·leetcode·面试·蓝桥杯·学习方法
摇滚侠15 小时前
《On Java 中文版 基础卷》阅读笔记 接口 19
java·开发语言·笔记