我是靠谱客的博主 无聊短靴,这篇文章主要介绍重载实例,现在分享给大家,希望可以做个参考。

package ray;

public class ChongZai {
	public static void main(String []args) {
		System.out.println(add(1,2));
		System.out.println(add(1,2,3));
		System.out.println(add(1.1,2.1));
		System.out.println(add(1.1,2));
		System.out.println(add(1,2.1));
	}
	public static int add(int a,int b ) {
		int c=a+b;
		return c;
	}
	//方法名相同 参数个数不同 构成重载
	public static int add(int a,int b,int c) {
		int d=a+b+c;
		return d;
	}
	//方法名相同 参数类型不同 构成重载
	public static double add(double a,double b) {
		double c=a+b;
		return c;
	}
	//方法名相同 参数顺序不同 构成重载
	public static double add(double a,int b) {
		double c=a+b;
		return c;
	}
	public static double add(int a,double b) {
		double c=a+b;
		return c;
	}
	//只有返回值类型不同 不构成重载的方法
	/*public static double add(int a,int b) {
		double c=a+b;
		return c;
	}
	//只有参数名称不同 不构成重载的方法
	public static double add(int a1,int a2) {
		double c=a+b;
		return c;
	}*/
}

在这里插入图片描述

最后

以上就是无聊短靴最近收集整理的关于重载实例的全部内容,更多相关重载实例内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(137)

评论列表共有 0 条评论

立即
投稿
返回
顶部