Java study notes (12): member methods

basic introduction

In some cases, we need to define member methods (methods for short). For example, human beings: In addition to some attributes (age, name..), we humans also have some behaviors such as: we can talk, run..., through learning, we can also do arithmetic problems. At this time, it is necessary to use member methods to complete. Now it is required to complete the Person class.

Definition of member methods

public (access modifier) ​​return data type method name ( parameter list.. ) {//method body statement; return return value;

}

explain:


(1) Formal parameter list: Indicates member method input 
eg: person(int n), getSum(int num1, int num2)

(2) Return data type: indicates member method output , void indicates no return value

(3) Method body: Indicates that in order to realize a certain functional code block

(4) The return statement is not necessary .

Quick Start Case

(1) Add the speak member method to output "I am a good person"

public class Method { 

	//编写一个main方法
	public static void main(String[] args) {
		Person p1 = new Person();//创建对象 
		p1.speak(); //调用方法
	}
}

class Person {
	public void speak() {
		System.out.println("我是一个好人");
	}

	
}

 

(2) Add cal01 member method, which can calculate the result from 1+..+1000

public class Method { 

	//编写一个main方法
	public static void main(String[] args) {
		Person p1 = new Person();//创建对象
		p1.cal01(); //调用cal01方法
	}
}

class Person {
	public void cal01() {
		int res = 0;
		for(int i = 1; i <= 1000; i++) {
			res += i;
		}
		System.out.println("cal01方法的计算结果=" + res);
	}

	
}

 

(3) Add cal02 member method, which can receive a number n and calculate the result from 1+..+n

public class Method { 

	//编写一个main方法
	public static void main(String[] args) {
		Person p1 = new Person();//创建对象
		p1.cal02(5); //调用cal02方法,同时给n = 5
		p1.cal02(10); //调用cal02方法,同时给n = 10
	}
}

class Person {
	public void cal02(int n) {
		//循环完成
		int res = 0;
		for(int i = 1; i <= n; i++) {
			res += i;
		}
		System.out.println("cal02方法 计算结果=" + res);
	}

	
}

 

(4) Add the getSum member method, which can calculate the sum of two numbers 

public class Method { 

	//编写一个main方法
	public static void main(String[] args) {
		Person p1 = new Person();//创建对象
		/*把 方法 getSum 返回的值,赋给 变量 returnRes
		然后调用getSum方法,同时num1=10, num2=20
			*/
		int returnRes = p1.getSum(10, 20); 
		System.out.println("getSum方法返回的值=" + returnRes);
	}
}

class Person {
	public int getSum(int num1, int num2) {
		int res = num1 + num2;
		return res;//返回res的值
	}

	
}

 Benefits of member methods

(1) Improve code reusability

(2) The details of the implementation can be encapsulated and then called by other users

Why do you need member methods 

Reference requirements:

Please traverse an array and output the value of each element of the array.

Solution 1: The traditional method is to use a single for loop to output the array.

Solution 2: Define a class MyTools, then write a member method, and call the method to achieve

public class Method {


//编写一个 main 方法
public static void main(String[] args) {



int [][] map =	{
   
   {0,0,1},{1,1,1},{1,1,3}};//遍历数组 , 输出数组的各个元素值


//遍历 map 数组
for(int i = 0; i < map.length; i++) {
    for(int j = 0; j < map[i].length; j++) {
        System.out.print(map[i][j] + "\t");
    }
System.out.println();
    }

}
}

This is the traditional method, traversing directly, but if it is required to traverse the map array again, is it necessary to write a for loop? It’s okay to traverse once or twice, but if the company requires 100 or 1000 traverses, wouldn’t it be necessary to write 100 or 1000 for loops? This is too inefficient and laborious, and the code is not very readable, so we need to use the method to complete the output at this moment:

public class Method {


//编写一个 main 方法
public static void main(String[] args) {



int [][] map =	{
   
   {0,0,1},{1,1,1},{1,1,3}};//遍历数组 , 输出数组的各个元素值

MyTools tool = new MyTools();//创建 MyTools 对象

tool.printArr(map);//使用方法 

class MyTools {
    public void printArr(int[][] map) { 
        System.out.println("=======");

for(int i = 0; i < map.length; i++) {//对map数组进行遍历输出
    for(int j = 0; j < map[i].length; j++) { 
        System.out.print(map[i][j] + "_");
}
System.out.println();
}
}
}

The method tool.printArr(map) is used here ; at this moment, a lot of effort can be saved to perform for traversal, which is more efficient than traditional methods, saving time and effort

Guess you like

Origin blog.csdn.net/long_0901/article/details/124282927