Java learning summary Lesson

A: Java overloading Comments

The same class have the same method but different parameter lists multiple methods name, a phenomenon known as method overloading.

A list of different parameters which include the following scenario:

  • The number of different parameters
public class Computer
{
	public int add(int a, int b)
	{
		return a + b;
	}
	public int add(int a, int b, int c)
	{
		return a + b + c;
	}
}
  • Different types of parameters corresponding to
public class Computer
{
	public int add(int a, int b)
	{
		return a + b;
	}
	public double add(double a, double b)
	{
		return a + b;
	}
}

note:

  • Different parameter list does not contain the names of different parameters, that is, if the same method name, process parameters and the number of the same type, but different parameter name, so that the method can not be called reload.
  • The method does not participate in comparison to other components: access specifier, modifiers, return type.

A plurality of class method with the same name (or normal constructor method), when calling these methods, in the end of which depends on the data type calls and the number of passed parameters when invoked method.

public class Teacher {

	public static void print(int age, String name) 
	{
		System.out.println(age + "," + name);
	}

	public static void print(String name, int age) 
	{
		System.out.println(name + "," + age);
	}

	public static void main(String [] args) 
	{
		print(33, “王小红”);// 依次传入int类型和String类型数据,所以调用第一个方法
		print("王小红", 33);//依次传入String类型和int类型数据,所以调用第二个方法
	}
}

Two: Java return value Comments

Method for the return operation when:

  • The method returns the type of the specified value, the object may be
  • Method end

return to the form in which method:

  • There return type: return i;
int add(int x, int y)
{
	return x + y;
}
  • None Return Type: return;

General methods void former representing no return value, the return value is no void.

void name(){
	System.out.println("FOREVER_GWC");
}

Released six original articles · won praise 0 · Views 48

Guess you like

Origin blog.csdn.net/FOREVER_GWC/article/details/104599327