Java self-learning day10 - object-oriented summary (Continued)

Posted by dasmon777 on Fri, 18 Feb 2022 03:10:53 +0100

Class structure 2: method

Class structure 2: method
1. Concept of method overloading
Definition: more than one method with the same name is allowed in the same class, as long as their parameter number or parameter type are different.
*
Summary: "two are the same and different": the same class and the same method name
Different parameter lists: different parameter numbers and different parameter types

2. Examples of heavy load:
Example 1: overloaded sort() / binarySearch() in Arrays class; println() in PrintStream
Example 2:

//The following four methods constitute overloads
	public void getSum(int i,int j){
		System.out.println("1");
	}
	
	public void getSum(double d1,double d2){
		System.out.println("2");
	}
	
	public void getSum(String s ,int i){
		System.out.println("3");
	}
	
	public void getSum(int i,String s){
		System.out.println("4");
	}
   
Examples that do not constitute overloading:
	//The following three methods cannot be overloaded with the above four methods
//	public int getSum(int i,int j){
//		return 0;
//	}
	
//	public void getSum(int m,int n){
//		
//	}
	
//	private void getSum(int i,int j){
//		
//	}

3. How to judge whether it constitutes a method overload?
Judge strictly according to the definition: two are the same and different.
It has nothing to do with the permission modifier, return value type, formal parameter variable name and method body of the method!
4. How to determine the call of a method in a class:
Method name - > parameter list

Interview question: what is the difference between method overloading and rewriting?
throws\throw
String\StringBuffer\StringBuilder
Collection\Collections
final\finally\finalize
...
Abstract classes and interfaces
sleep() / wait()

Method of variable number formal parameters

1. Instructions:

  • 1. New content in JDK 5.0
  • 2. Specific use:
  • 2.1 format of variable number formal parameters: data type... Variable name
  • 2.2 when calling the method with variable number of formal parameters, the number of parameters passed in can be: 0, 1, 2,...
  • 2.3 the methods of variable number formal parameters have the same name as the methods in this class, and methods with different formal parameters constitute overloads
  • 2.4 the methods of variable number formal parameters are the same as those in this class, and the arrays with the same type of formal parameters do not constitute overloading. In other words, the two cannot coexist.
  • 2.5 variable number formal parameters must be declared at the end in the formal parameters of the method
  • 2.6 variable number formal parameters in the formal parameters of a method, at most one deformable parameter can be declared.

2. Examples:

 	public void show(int i){
		
	}
	
	public void show(String s){
		System.out.println("show(String)");
	}
	
	public void show(String ... strs){
		System.out.println("show(String ... strs)");
		
		for(int i = 0;i < strs.length;i++){
			System.out.println(strs[i]);
		}
	}
	//Cannot exist with previous method
//	public void show(String[] strs){
//		
//	}
When called:
		test.show("hello");
		test.show("hello","world");
		test.show();
		
		test.show(new String[]{"AA","BB","CC"});

Value Passing Mechanism of java

1. Examples of assignment of variables in the method:

		System.out.println("***********Basic data type:****************");
		int m = 10;
		int n = m;
		
		System.out.println("m = " + m + ", n = " + n);
		
		n = 20;
		
		System.out.println("m = " + m + ", n = " + n);
		
		System.out.println("***********Reference data type:****************");
		
		Order o1 = new Order();
		o1.orderId = 1001;
		
		Order o2 = o1;//After the assignment, the address values of o1 and o2 are the same, and both point to the same object entity in the heap space.
		
		System.out.println("o1.orderId = " + o1.orderId + ",o2.orderId = " +o2.orderId);
		
		o2.orderId = 1002;
		
		System.out.println("o1.orderId = " + o1.orderId + ",o2.orderId = " +o2.orderId);
		

Rules:
If the variable is a basic data type, the data value saved by the variable is assigned at this time.
If the variable is a reference data type, the value assigned at this time is the address value of the data saved by the variable.

2. Parameter concept for method
Formal parameter: the parameter in parentheses declared during method definition
Argument: the data actually passed to the formal parameter when the method is called

**3. Parameter transfer mechanism in Java: value transfer
Rules:

  • If the parameter is a basic data type, what the argument assigns to the formal parameter is the data value actually stored by the argument.
  • If the parameter is a reference data type, the address value of the stored data of the parameter is assigned to the formal parameter**

extension:
If the variable is a basic data type, the data value saved by the variable is assigned at this time.
If the variable is a reference data type, the value assigned at this time is the address value of the data saved by the variable.

4. Typical examples and memory analysis:
[example 1]

[example 2]

Recursive Method
1. Definition:
Recursive method: a method body calls itself.
2. How to understand recursive methods?

Method recursion contains an implicit loop that repeats a piece of code without loop control.
Recursion must recurse in the known direction, otherwise this recursion will become infinite recursion, similar to an endless loop.

3. Examples:

// Example 1: calculate the sum of natural numbers between 1-n

	public int getSum(int n) {// 3

		if (n == 1) {
			return 1;
		} else {
			return n + getSum(n - 1);
		}

	}

	// Example 2: calculate the product of natural numbers between 1-n: n!
	public int getSum1(int n) {

		if (n == 1) {
			return 1;
		} else {
			return n * getSum1(n - 1);
		}

	}
	
	//Example 3: we know a sequence: f(0) = 1,f(1) = 4,f(n+2)=2*f(n+1) + f(n),
	//Where n is an integer greater than 0, find the value of f(10).
	public int f(int n){
		if(n == 0){
			return 1;
		}else if(n == 1){
			return 4;
		}else{
//			return f(n + 2) - 2 * f(n + 1);
			return 2*f(n - 1) + f(n - 2);
		}
	}

	//Example 4: Fibonacci sequence
	
	//Example 5: Hanoi Tower problem
	
	//Example 6: fast exhaust