方法重载:
package com.wys.java; public class OverrideDemo { //方法重载 //求两个int数字的和 public int plus(int m, int n){ return n+m; } //求两个double型数字的和 public double plus(double m, double n){ return n+m; } //求数组元素的累加和 public int plus(int[] arr){ int sum=0; for(int i=0; i<arr.length; i++){ sum += arr[i]; } return sum; } public static void main(String[] args) { int m=5,n=10; int[] arr = {1,2,3,4,5}; OverrideDemo overrideDemo = new OverrideDemo(); //创建对象 System.out.println("int 类型的和:"+overrideDemo.plus(m,n)); System.out.println("double 类型的和:"+overrideDemo.plus(4.6, 6.6)); System.out.println("数组元素的和:"+overrideDemo.plus(arr)); } }
结果: