Exercise 5 java object oriented method overloading and reference deformable

/*

  • Overloaded method:
  • 1 in the same class with the same name there is a method to allow more than one, provided that they have different number of parameters or parameter type
  • (Two different the same: the same class, same name, different parameter list)
  • 2 Overload characteristic: irrespective of the type of the return value, but only the parameter list, the parameter list must be different (the number of parameters and parameter types)
  • When called, the method according to the parameter list to distinguish between different methods
  • 3 determines whether overload: permission modifier with the method, the return type, shape parameters of the variable name, are independent of the method body.
  • Only two are the same different Analyzing
  • 4 When the object by calling the method, how to determine what method call is:
  • See the method name
  • See parameter list
  • Example:
  • int add (int x, int y) {// calculate two parameters and
  • return x + y;
  • }
  • int add (int x, int y, int z) {// calculate three parameters and
  • return x + y +z;
  • }
  • double add (double x, double y) {// float calculates two parameters, and
  • return x + y;
  • }
    */

/*

  • A variable number of parameter
  • Providing javaSE5.0 Varargs (variable number of arguments) Mechanism
  • Energy and allows to directly define a plurality of arguments match parameter. Which can use a simple manner a variable number of arguments passed
  • JDK5.0 previously used to define the array parameter, passing a plurality of variables of the same type
  • public static void test(int a ,Sting[] books){}
  • After a variable number of parameter JDK5.0 defined, passing a plurality of variables of the same type
  • public static void test(int a ,Sting…books){}
  • Reference deformable use
  • 1, format ... :( data type variable name)
  • 2, when there are a variable number of parameter method call, the number of parameters passed may be 0, 1 or more.
  • 3, when the process variable number of parameter names with the same method in this class, parameter configuration among overloaded methods.
  • 4, when the number of the variable parameter between a method with the same name of the method in this class, the same parameter type array does not constitute a heavy load, the two can not coexist
  • 5, a process, a variable number of at most one parameter, and the statement at the end of the argument list. (If the front, or a plurality of deformable parameters, the input parameters after the compiler does not know whether deformable parameters, which parameters can be modified, no such problem with array belong)
    * /
package com.atguigu.contact;
import java.util.*;
public class Object5 {
public static void main(String[] args) {
	Scanner scan = new Scanner(System.in);
	//测试重载
	Count c = new Count();
	c.sum(1, 2);//输出1
	//注意:如果sum(int i,int j)方法被注释掉,则因为自动类型提升会执行
	//sum(double i,double j),输出2
	
	OverLoad o = new OverLoad();
    o.MathTest(11, 22);
	o.Max(3.1, 1.2, 9.2);
	//测试可变形参
	Argu a = new Argu();
	a.show("AA");
	a.show("AA","BB");
	//a.show();//两个方法都用可变形参,编译器不确定使用哪个方法
	a.show(1);
	a.show(23,56,12,45);
	a.show(new int[] {});//可变形参方法可以直接填入数组
}
}

class Count{
	public void sum(int i,int j) {
		System.out.println("1");
	}
	public void sum(double i,double j) {
		System.out.println("2");
	}
	public void sum(int i,double j) {
		System.out.println("3");
	}
	public void sum(double j,int i) {
		System.out.println("4");
	}
	public void sum() {
		System.out.println("5");
	}
//	public int sum(int i,int j) { //与是否返回无关,不能构成重载
//		return 0;
//	}
//	public void sum(int m,int n) { //与形参名无关,不能构成重载
//		System.out.println("5");
//	}
//	private void sum(int i,int j) {//与修饰符无关,不能构成重载
//		System.out.println("5");
//	}
//	private void sum(int i,int j) {//与方法体无关,不能构成重载
//	System.out.println();
//}
	
}

class OverLoad{
	public void MathTest(int i) {
		System.out.println(i * i);
	}
	public void MathTest(int i,int j) {
		System.out.println(i * j);
	}
	public void MathTest(double i) {
		System.out.println(i);
	}
	public void Max(int i,int j) {
		int max;
		max = (i >= j)?i:j;
		System.out.println(max);
	}
	public void Max(double i,double j) {
		double max;
		max = (i >= j)?i:j;
		System.out.println(max);
	}
	public void Max(double i,double j,double l) {
		double max;
		max = (i >= j)?i:j;
		max = (max >= l)?max:l;
		System.out.println(max);
	}
}

class Argu{
	public void show(String s) {
		System.out.println("传入了一个数据" + s);
	}
	public void show(String ... s ) {//实际还是创建一个String类型的数组,数组名s,只是语法改变
		System.out.println("又传入了一个数据" + s);//会输出数组s 的首地址值。在方法使用时输入完变量后确定数组地址值与长度
		for (int i = 0; i < s.length; i++) {//遍历输出,其他功能与数组的操作相同
			System.out.println("又传入了一个数据" + s[i]);
		}
	}
//	public void show(String[] s) {             //与可变形参重复,不构成重载
//		System.out.println("又传入了一个数据" + s);
//	}
	public void show(int s) {
		System.out.println("传入了一个数据" + s);
	}
	public void show(int ... s) {
		int sum = 0;
		if(s.length != 0) {
		for (int i = 0; i < s.length; i++) {
			sum += s[i];
		}
		System.out.println("传入数据总和是" + sum);
		}else {
			System.out.println("没有传入数据");
		}
	}
}
Published 47 original articles · won praise 1 · views 1075

Guess you like

Origin blog.csdn.net/wisdomcodeinside/article/details/103999719