当前位置:网站首页>jvm: 方法重载时,具体调用哪个方法,是由传入参数的静态类型来决定的,而不是由参数的实际类型来决定
jvm: 方法重载时,具体调用哪个方法,是由传入参数的静态类型来决定的,而不是由参数的实际类型来决定
2022-06-23 03:58:00 【amadeus_liu2】
一、示例
package com.example.controller;
public class TestOverload {
public static void main(String[] args) {
Grandpa grandpa=new Grandpa();
Grandpa father=new Father();
Grandpa offspring=new Offspring();
print(grandpa);
print(father);
print(offspring);
}
public static void print(Grandpa grandpa){
System.out.println("print grandpa......");
}
public static void print(Father father){
System.out.println("print father......");
}
public static void print(Offspring offspring){
System.out.println("print offspring......");
}
}
class Grandpa{
}
class Father extends Grandpa {
}
class Offspring extends Father{
}
二、运行输出:
三、分析:
grandpa、father、offspring这3个对象的静态类型都是Grandpa,它们的实际类型分别是Grandpa、Father、Offspring,
将这3个对象传递给print方法时,因为它们的静态类型为Grandpa,所以都会调用
public static void print(Grandpa grandpa){
System.out.println("print grandpa......");
}
边栏推荐
- Xa mode explanation and code implementation of four Seata modes
- hash---------history
- Memory model of JVM principle
- Composite API
- View of MySQL introductory learning (III)
- 抽奖 ddd 代码
- LeetCode-1757. 可回收且低脂的产品_SQL
- Fs2119a Synchronous Boost IC output 3.3V and fs2119b Synchronous Boost IC output 5V
- [microservices | Nacos] Nacos realizes data isolation of multi environment and multi tenant
- Face recognition determination threshold
猜你喜欢

Jenkins installs and deploys and automatically builds and publishes jar applications

Complete one-time GC process of JVM principle

Software project management 8.4 Software project quality plan

Xa mode explanation and code implementation of four Seata modes

MCS:连续随机变量——LogNormal分布

牛B程序员在“创建索引”时都会注意啥?

Redis缓存穿透解决方案-布隆过滤器

MCS:连续随机变量——Student’s t分布

Fs2119a Synchronous Boost IC output 3.3V and fs2119b Synchronous Boost IC output 5V

Differences between fs4059a and fs5080e charging chips
随机推荐
Today's sleep quality record 80 points
CF [1700d] D. River locks (DP, bisection, Mathematics)
[opencv450] image subtraction, binarization, threshold segmentation
Is there a real part-time job online? How do college students find part-time jobs in summer?
关于重放攻击和防御
Konva series tutorial 1:what is konva?
数学分析_笔记_第1章:集合与映射
Un processus GC complet pour le principe JVM
QT QWidget nesting relative position acquisition (QT drawing nesting)
Qimen dunjia assistant decision software
Win11应用商店一直转圈解决办法
Cloud native database is the world of future databases
GDB data reading in GDAL (III) of GIS
Redis缓存穿透解决方案-布隆过滤器
After the idea code is developed, the code is submitted. If the branch is found to be incorrect after submission, how can I withdraw it
Implementation of MySQL custom sequence number
View of MySQL introductory learning (III)
软件设计开发笔记2:基于QT设计串口调试工具
MCS: continuous random variable lognormal distribution
LeetCode-1757. 可回收且低脂的产品_SQL