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;
	}
}
相关推荐
小短腿的代码世界5 分钟前
Qt国际化深度解析:从源码到企业级多语言实践
java·数据库·qt
江上清风山间明月11 分钟前
如何将python开发的window应用打包成exe
开发语言·python·exe·打包
凌冰_15 分钟前
IDEA 集成Claude Code
java·ide
SXJR17 分钟前
Java中的Cross-Encoder模型解决方案
java·开发语言
彦为君26 分钟前
JavaSE-11-BIO/NIO/AIO(多人聊天室)
java·开发语言·python·ai·nio
为何创造硅基生物28 分钟前
C 语言 typedef 结构体私有化
c语言·开发语言·算法
计算机安禾33 分钟前
【c++面向对象编程】第43篇:可变参数模板(C++11):优雅处理不定长参数
java·开发语言·c++
Hanniel38 分钟前
Python __slots__ 入门指南
开发语言·python·性能优化
AI人工智能+电脑小能手41 分钟前
【大白话说Java面试题 第69题】【JVM篇】第29题:GC Roots 有哪些?
java·开发语言·jvm·面试
William Dawson1 小时前
【通俗易懂!Spring四大核心注解源码解读:@Configuration、@ComponentScan、@Import、@EnableXXX实战】
java·后端·spring