<>1 method

<>1.1 summary

Named code block , Methods can have parameters or no parameters , It can improve the reusability of code .

<>1.2 Format of method definition

<>1.3 Method call sequence diagram

Sequential code execution , Call the specified method , completion of enforcement , Return to call location

<>1.4 practice : Call order of test methods / parameter / Return value

create package :cn.tedu.method
Create class :TestMethod .java
package cn.tedu.method; /** This class is used to test methods */ public class TestMethod {
//1. Create the entry function of the program main() public static void main(String[] args) { System.out.println(
1); /**2. We use the method name + Parameter list to call the function of the method */ method1();// call method1() System.out.println(2)
; method2(3);// call method2() int result = method3(1,2);// call method3() System.out.
println(result); } /**3. If the method wants to return a value , The return value type must be modified * also return Results of corresponding types *
If the return value type of the method is void, Return value is not allowed * */ /* This method is used to test the return value type of the method */ public static int method3(int i
, int j) { /**4. adopt return Keyword returns the method result to the call location */ return i+j; } /**1. Modifier for method The return value type of the method
Method name ( Method parameters ){ Method body }*/ /*method1() You want to test the call order of methods */ public static void method1() { System.
out.println(5); System.out.println(6); System.out.println(7); }
/* This method is used to test the parameters of the method , The position of the parameter is in parentheses */ public static void method2(int a) { System.out.
println(" SpongeBob this year :"+ a +" I'm ten years old ~"); } }
<>1.5 Overload of method

Method overloading refers to defining multiple methods with the same name in a class , But the parameter list for each method is different ( That is to say, the number and type of parameters are different ), When the program calls a method , You can decide which method to call by passing them different numbers and types of parameters .

<>1.6 practice : Overloading of test methods

create package : cn.tedu.method
Create class : TestMethodOverload.java
package cn.tedu.method; /** This class is used for overloading test methods */ public class TestMethodOverload {
public static void main(String[] args) { /**1. We use the method name + The parameter list determines which method to call */
/**2. Overload of method : * In the same class , The existing methods have the same name , But the parameter list is different * If in the same class , The number of parameters of a method with the same name is different , Must constitute a heavy load *
If in the same class , Methods with the same name have the same number of parameters , * You need to check the type of the parameter in the corresponding position , Instead of the parameter name , Independent of parameter name * (int a,String b) And (int
b,String a)-- No overload * (int a,String b) And (String a,int b)-- Constitute heavy load * */ //2. call method()
method(); //4. call method(int) method(666); //6. call method(int,String) method(888,
" Bubbles "); } //1. Create a parameterless method() public static void method() { System.out.println(
" Ha ha ha ha, I have no parameters "); } //3. Create a method(int n) public static void method(int n) { System.
out.println(" Ha ha ha, my parameters are :"+n); } //5. Create a method(int n,String s) public static void
method(int a,String b) { System.out.println(b+" I'm going to fuck him tonight "+a+" A bowl of rice "); } public static
void method(String a,int b) { System.out.println(b+" I'm going to fuck him tonight "+a+" A bowl of rice "); } }
<>2 variable

<>2.1 concept

Number that can be changed , It's called a variable . stay Java In language , All variables must be declared before use .
Generally through “ Variable type Variable name = Variable value ;” These three parts describe a variable . as :int a = 3 ;
The principle of using variables : Principle of proximity , That is to minimize the use of variables

<>2.2 local variable

position : Defined in methods or local code blocks
be careful : You must initialize manually to allocate memory . as :int i = 5; perhaps int i; i = 5;
Scope : That is, in methods or local code blocks , After the method runs, the memory is released

<>2.3 Member variable

position : Defined in a class but not in a method
be careful : No initialization , It is also automatically initialized to the default value
Scope : In the whole class , The class disappeared , Variables are released

<>2.4 practice : Local variable and member variable testing

create package : cn.tedu.basic
Create class : TestVariable.java
package cn.tedu.oop; /** This class is used to test the use of variables */ public class TestVariable { //2. Define member variables :
//1) position : Inside class and outside method //2) No manual initialization is required , The default value of the corresponding type is automatically assigned //3) Scope : Takes effect throughout the class , The class disappears , The variables disappear static int
count; //3. There is a principle for using variables : Principle of proximity static int sum = 200; public static void main(String[
] args) { //1. Define local variables : //1) position : In the method / Local code block //2) It must be initialized manually
//3) Scope : In the method / In local code block , After the corresponding code is executed, the local variables are released int sum = 100;// Local variables defined in methods sum System.out.
println(sum);// The principle of variable proximity : You use variables near you ,100 System.out.println(count); for (int i =
0; i < 10; i++) {// local variable i Can only be used in a loop System.out.println(i); }
//System.out.println(i);// report errors : Cannot reference variable i:i cannot be resolved to a variable } }

©2019-2020 Toolsou All rights reserved,
Huawei 2021 session Hardware Engineer Logical post (FPGA) Super detailed surface !!!Vue-element-admin upgrade ui edition virtual machine VMware Download and install the most detailed tutorial !C++ Move constructor and copy constructor sound of dripping water java Backstage interview pygame Realize full screen mode and adjustable window size mysql Database setting character set configuration modification my.ini file (windows)30 What's the experience of being a junior programmer at the age of 20 C++ Multithreading programming ( Summary of common functions and parameters )python_ cherry tree