java object-oriented exercise 5 method overload and deformable parameters

Posted by mrjameer on Thu, 16 Jan 2020 03:15:36 +0100

/*

  • Method overload:
  • 1 More than one method with the same name is allowed in the same class if their number of parameters or parameter types are different
  • (Same class, same name, different parameter list)
  • 2 Overload feature: independent of the type of return value, just look at the parameter list, the parameter list must be different (number of parameters and parameter type)
  • When invoked, methods are distinguished based on the list of method parameters
  • 3 Determine whether overload occurs: it has nothing to do with the permission modifier of the method, the return value type, the variable name of the parameter, or the body of the method.
  • Use only two identical but different judgments
  • 4 How to determine what method is called when calling a method through an object:
  • Look at method names
  • Look at the parameter list
  • Example:
  • int add(int x,int y) {//Calculates the sum of two parameters
  • return x + y;
  • }
  • int add(int x,int y,int z) {//Calculates the sum of three parameters
  • return x + y +z;
  • }
  • double add(double x,double y) {//Calculates the sum of two floating-point parameters
  • return x + y;
  • }
    */

/*

  • Variable number of parameters
  • javaSE5.0 provides a Varargs (variable number of arguments) mechanism
  • Allows direct definition of parameters that can match multiple arguments.This allows you to pass a variable number of arguments in a simpler way
  • JDK5.0 used array parameters to define methods that passed in multiple variables of the same type
  • public static void test(int a ,Sting[] books){}
  • After JDK5.0, methods are defined using variable number parameters, passing in multiple variables of the same type
  • public static void test(int a ,Sting...books){}
  • Use of Variable Parameters
  • 1, Format: (Data Type...Variable Name)
  • 2. When a method with variable number of parameters is called, the number of incoming parameters can be zero, one, or more.
  • 3. When methods with variable number of parameters have the same method name as those in this class, overloads are formed between methods with different parameters.
  • 4. When the method with variable number of parameters is the same as the method name in this class and the array with the same parameter type does not constitute an overload, they cannot coexist
  • 5. In a method, there can be at most one variable number parameter and it is declared at the end of the parameter list.(If, in the previous case, or if there are more than one variable parameter, the compiler does not know whether or not the subsequent input parameter belongs to a variable parameter and which variable parameter belongs, there is no problem using arrays)
    */
package com.atguigu.contact;
import java.util.*;
public class Object5 {
public static void main(String[] args) {
	Scanner scan = new Scanner(System.in);
	//Test overload
	Count c = new Count();
	c.sum(1, 2);//Output 1
	//Note: If the sum(int i,int j) method is commented out, automatic type promotion will be performed
	//sum(double i,double j), output 2
	
	OverLoad o = new OverLoad();
    o.MathTest(11, 22);
	o.Max(3.1, 1.2, 9.2);
	//Test Variable Parameters
	Argu a = new Argu();
	a.show("AA");
	a.show("AA","BB");
	//a.show(); //Both methods use variable parameters, and the compiler is not sure which method to use
	a.show(1);
	a.show(23,56,12,45);
	a.show(new int[] {});//Variable parameter methods can fill arrays directly
}
}

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) {// is independent of whether or not to return and cannot constitute an overload
//		return 0;
//	}
//	public void sum(int m,int n) {// has nothing to do with parameter names and cannot constitute an overload
//		System.out.println("5");
//	}
//	private void sum(int i,int j) {// has nothing to do with modifiers and cannot constitute an overload
//		System.out.println("5");
//	}
//	private void sum(int i,int j) {// is independent of the method body and cannot constitute an overload
//	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("A data was passed in" + s);
	}
	public void show(String ... s ) {//Actually, create an array of type String with the array name s, just a syntax change
		System.out.println("Another data was passed in" + s);//The first address value of the array s is output.Determine the array address value and length after entering variables when using the method
		for (int i = 0; i < s.length; i++) {//Traverse the output, other functions are the same as operations on arrays
			System.out.println("Another data was passed in" + s[i]);
		}
	}
//	public void show(String[] s) {// duplicates a deformable parameter and does not constitute an overload
//		System.out.println("another data passed in" + s);
//	}
	public void show(int s) {
		System.out.println("A data was passed in" + 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("The sum of incoming data is" + sum);
		}else {
			System.out.println("No incoming data");
		}
	}
}
Nineteen original articles published. Zambia 1. Visits 192
Private letter follow

Topics: Java