当前位置: 首页 > news >正文

Java基础 4.12

1.方法的重载(OverLoad)

基本介绍

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

重载的好处

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

2.重载的快速入门 

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;
	}
}

相关文章:

  • 《jQuery EasyUI 插件》
  • Linux基础5
  • 【数据结构与算法】ArrayList 和 顺序表
  • Python进阶(3):函数(接上篇)
  • Linux基础8
  • Linux磁盘大师:存储管理完全指南
  • Redis 哨兵模式 搭建
  • PDF 转换为 Word、HTML、LaTeX 和 Markdown 格式
  • TDengine 语言连接器(C/C++)
  • 智能手机功耗测试
  • jxls2.10实现模板导出/单元格合并/自定义标签实现单元格隐藏
  • 为什么ASCII的A是65[特殊字符]
  • NET模式下如何配置虚拟机的IP地址为静态的
  • 在Ubuntu 22.04里使用KVM创建虚拟机Ubuntu 22.04
  • 【源码】Mybatis源码
  • 图片文本识别OCR+DeepSeekapi实现提取图片关键信息
  • Linux权限理解
  • [IEEE TIP 2024](cv即插即用模块分享)IdeNet信息增强模块 性能提升必备!
  • 如何用海伦公式快速判断点在直线的哪一侧
  • MCP(模型上下文协议)简单案例
  • 个人网站建设存在哪些问题/青岛模板建站
  • 私人app一键生成器/seo外链怎么做能看到效果
  • 广东南方建设工程有限公司网站/百度网址大全手机版
  • .net网站开发实训/线上营销方式
  • 服装网站建设公司好吗/域名查询访问
  • 电脑版商城网站建设/免费建站模板