Functions are also called methods!
Functions and features:
1. Used to define functions and encapsulate functions.
2. It can improve the reusability of the code.
Function notes:
1. Function application cannot be performed (functions cannot be defined within functions).
2. Functions can only be executed if they are called.
3. Function types modified by basic data types (String, int,….) must have a return return value.
4. For functions modified by void, the return statement in the function can be omitted and not written.
5. Function names can be named according to requirements.
Code example: (with or without the difference between functions/methods)
No function/method code example:
public class NoFunc {public static void main(String[] args) { //main is also a function for program running int a=1;int b=2;int addSum=0;int mulSum=0;addSum=a+b;mulSum=a*b;System.out.println("add"+addSum);System.out.println("multiple"+mulSum);a=2; //Modify the value of a and do another operation addSum=a+b;mulSum=a*b;System.out.println("add"+addSum);System.out.println("mulSum"+mulSum);}}Examples of ordinary function/method code:
public class Func {int a=1; //a is the actual parameter int b=2;void Cal(int addSum,int mulSum){ //sum is the formal parameter addSum=a+b;mulSum=a*b;System.out.println("add"+addSum); System.out.println("multiple"+mulSum); //void has no return value}//Modify the value of a and do another operation int setA(int a){ //a is the formal parameter this.a=a; //The actual parameter is assigned to the formal parameter return a; //return return value a}public static void main(String[] args) { //main is also a function used to run Func f=new Func(); //Create object f.Cal(0,0); //The object calls the Add function, and 0 is assigned to sum(initialize)f.setA(2); //A is assigned to 2f.Cal(0,0); //Do an operation elsewhere}}Running result: (same)
Addition 3
Multiplication 2
Addition 4
Multiplication 4
Function classification:
1. Ordinary functions
2. Constructor
3. Main function (special)
Constructor Notes:
1. The method name of the constructor must be the same as the class name.
2. The function type cannot be declared, there is no return type, and it cannot be defined as void.
3. There cannot be any non-accessory modifier modifications, such as static, final, synchronized, and abstract, which cannot modify the constructor.
4. The constructor cannot be called directly, it must be called through the new keyword.
The function of the constructor:
1. Convenient to pass parameters.
2. Initialize the object by calling the constructor through new. It is to initialize the object that matches its format (parameter list).
Example constructor code:
public class Constructor { int a=233; int b=233; Constructor(){ //No parameter constructor} Constructor(int a,int b){ //Argument constructor this.a=a; this.b=b; } void Cal(){ int addSum=a+b; int mulSum=a*b; System.out.println("add"+addSum); System.out.println("multiple"+mulSum); //void has no return value} //Modify the value of a and perform another operation int setA(int a){ //a is the formal parameter this.a=a; //The actual parameter is assigned to the formal parameter return a; //Return return value a } public static void main(String[] args) { Constructor c1=new Constructor(); //Object created by the unargument constructor c1.Cal(); //The unargument constructor object calls the Cal function Constructor c2=new Constructor(1,2); //Object initialization c2.Cal(); //The parameter constructor object calls the Cal function c2.setA(2); //A is assigned to 2 c2.Cal(); //Or operation is done separately}}Running results:
Addition 466
Multiplication 54289
Addition 3
Multiplication 2
Addition 4
Multiplication 4