Java based self-study notes-Chapter 6: Methods

Chapter 6: Methods

1. Definition method

1. Define the method syntax:
modifier return value type method name (parameter list) { method body; } 2. Note


  • Method name and parameter list form method signature
  • Definition refers to what the defined item is, and declaration refers to the allocation of memory for the declared item to store data

2. Calling method

1. Attention

  • The method returning void must be a separate statement
  • The main method is similar to other methods, except that it is directly called by the Java virtual machine
  • Statements in the main method can call methods in the class where main is located, or methods in other classes
  • Although the following case is logically correct, it will compile errors
if(n>0) return 1;
else if(n==0) return 0;
else if(n<0) return -1;

//正确写法
if(n>0) return 1;
else if(n==0) return 0;
else return -1;

2.
void method void method does not require a return statement, but can terminate the method or return to the caller of the method

if(score<0||score>100)
System.out.println("分数必须在0-100之间");
return;

3. Parameter passing by value

1. The value of the actual parameter is passed to the formal parameter, no matter how the formal parameter changes, the value of the actual parameter will not change

int x=3;
System.out.println(x);//3
add(x);//调用add方法
System.out.println(x);//3
}
public static int add(int a) {
    
    
return a+3;//返回a+3
}

2. Attention

  • Variables cannot be defined repeatedly, some parameters in the parameter list cannot be defined

Four. Overload method

1. Features
have the same name, different parameter lists, which method is executed according to the method signature

2. Attention

  • The overloaded method must have a different parameter list
  • Methods cannot be overloaded based on different modifiers and return values
  • The three-parameter overload method can also call the two-parameter overload method
public static double max(double a,double b,double c){
    
    
return max(max(a,b),c);
}
public static double max(double x,double y){
    
    
return x>y?x:y;
}
  • Sometimes when a method is called, there are two or more matches, which will produce a compilation error
max(3,2);
public static double max(int a,double b){
    
    }
public static double max(double x,double y){
    
    }

Five. Classic case

1. Prompt the user to enter an integer, and then invert the number to output

public static void main(String[] args) {
    
    
		// TODO Auto-generated method stub
		Scanner in = new Scanner(System.in);
		int x = in.nextInt();
		int trans = transformation(x);
		System.out.println(x + "->" + trans);
	}

	public static int transformation(int x) {
    
    
		int result = 0;
		while (x != 0) {
    
    
			int y = x % 10;
			x /= 10;
			result = result * 10 + y;
		}
		return result;
	}

VI. Summary
Through the study of this chapter, I know how to define a method, call a method, pass parameters by passing parameters, and also understand overloading and the places that need to be paid attention to.

Come on! Chapter 7 To Be More...

Guess you like

Origin blog.csdn.net/weixin_42563224/article/details/104315430