Java基础 4.12

1.方法的重载(OverLoad)

基本介绍

Java中允许同一个类,多个同名方法的存在,但要求形参列表不一致! 如 System.out.println(); out是PrintStream类型

重载的好处

  1. 减轻了起名的麻烦
  2. 减轻了记名的麻烦

2.重载的快速入门

java 复制代码
public class OverLoad01 {
	public static void main(String[] args) {
		MyCalculator mc = new MyCalculator();
		System.out.println(mc.calculator(1, 1));
		System.out.println(mc.calculator(1, 1.1));
		System.out.println(mc.calculator(1.1, 1));
		System.out.println(mc.calculator(1, 1, 1));
	}
}

class MyCalculator {
	//下面的四个方法构成了重载
	public int calculator(int n1, int n2) {
		return n1 + n2;
	}

	public double calculator(int n1, double n2) {
		return n1 + n2;
	}

	public double calculator(double n1, int n2) {
		return n1 + n2;
	}

	public int calculator(int n1, int n2, int n3) {
		return n1 + n2 + n3;
	}
}
相关推荐
白杨尚青1 小时前
C++进阶篇(二):多态——虚函数、重写、虚函数表与三个经典坑(附完整代码仓库)
开发语言·c++·多态·虚函数·重写·动态绑定
考虑考虑7 小时前
JDK26中的Map.ofLazy
java·后端·java ee
鶴哥只手遮天11 小时前
Spring AI 2.0.1 深度解析-第 14 章 MCP 服务端(上):注解式实现与工具来源
java·人工智能·开源
鶴哥只手遮天11 小时前
Spring AI 2.0.1 深度解析-第 15 章 MCP 服务端(下):三种协议形态、Streamable 传输与状态管理
java·人工智能·开源
鶴哥只手遮天11 小时前
Spring AI 2.0.1 深度解析-第 16 章 向量存储(上):`VectorStore` 接口体系、`BatchingStrategy`
java·人工智能·开源
鶴哥只手遮天11 小时前
Spring AI 2.0.1 深度解析-第 12 章 工具循环的流式形态、Usage 累加与扩展体系
java·人工智能·开源
鶴哥只手遮天11 小时前
Spring AI 2.0.1 深度解析-第 23 章 模型供应商实现:从 `OpenAiSetup` 到 Bedrock
java·人工智能·开源
鶴哥只手遮天11 小时前
Spring AI 2.0.1 深度解析-第 06 章 内置 Advisor 全景:13 个实现逐个剖到源码级
java·人工智能·开源
鶴哥只手遮天11 小时前
Spring AI 2.0.1 深度解析-第 03 章 消息体系与 Prompt:不可变模型、`ChatOptions` 与模板渲染
java·人工智能·开源
鶴哥只手遮天11 小时前
Spring AI 2.0.1 深度解析-第 24 章 生产实践与 2.x 迁移:陷阱清单、AOT 与调优
java·人工智能·开源