Function:
Class Functiondemo {public static void main (string[] args) {/*int x = 4; System.out.println (x*3+5); x = 6; System.out.println (x*3+5); *///int y = 4*3+5;//int z = 6*3+5;//int x = GetResult (4);//system.out.println ("x=" +x);//int y = GetResult (6); GetResult (5);} The above calculation is found because the code is duplicated because of the results of the operation of the different data. To improve the reusability of the code. Extract the code. This section is defined as a separate function. Convenient and later use. The definition of functionality in Java is expressed in the form of functions. Need to define the function, complete an integer *3+5 operation,//1, first clarify the format of the function definition. /* Modifier Returns a value type function name (parameter type form parameter 1, parameter type form parameter 2,) {EXECUTE statement; return value;} When there is no specific return value after the function operation, this is the return value type identified with a special keyword. The keyword is void. void: Represents a case where the function does not have a specific return value. When the return value type of a function is void, the return statement in the function can be omitted from the write. */public static void getresult (int num) {System.out.println (num * 3 + 5); return;//can be omitted}}
Class FunctionDemo2 {public static void main (string[] args) {/*int sum = getsum (4,6); System.out.println ("sum=" +sum); sum = Getsum (2,7); System.out.println ("sum=" +sum); *///get (4,5); int x = getsum (bis); int y = Getsum (7,9); int num = Getmax (x, y);} /* This function defines the idea of a problem, why? Because only for the addition operation, if you want to do the printing operation, it is the caller's thing, do not do in the function. public static void get (int a,int b) {System.out.println (a+b); return;} *//* How to define a function? 1, since the function is a standalone function, then the result of the operation of the function is what is first clarified because this is the return value type in the explicit function. 2, whether unknown content is required to participate in the operation in the process of defining the function. Because it is in the argument list of the specified function (the type of the parameter and the number of arguments). *///requirements: Define a feature. Completes the 3+4 operation. and returns the result to the caller. /*1, the result of a clear function: is an integer and. 2, in the process of implementing this function, whether there is unknown content to participate in the operation, No. In fact, these two functions are defined in a definite function. 1, is the return value type in the explicit function. 2, define the parameter list of the function (the type of the parameter and the number of parameters). public static int Getsum () {return 3+4;} *//* The functions of this function, the result is fixed, without extensibility. In order to facilitate user needs. By the user to specify Addend and be addend. In this way, the function is meaningful. Idea: 1, the function result is a and. The return value type is int. 2, there is unknown content to participate in the operation. There are two of them. The type of this two unknown content is int. */public static int getsum (int x,int y) {return x+y;} /* Requirements: Determine whether two numbers are the same. Idea: 1, clear the result of the function: The result is: Boolean. 2, whether the function has unknown content to participate in the operation. There are, two integers. */public static Boolean compare (int a,int b) {/*if (a==b) return True;//elsereturn False;*///return (a==b)? True:false;return a==b;} /* Requirements: Define a function to compare two numbers. Gets the larger number. */public static int Getmax (int a,int b) {/*if (a>b) return A;elsereturn B;*/return (a>b)? A:b;}}
Overload
/* When do I use overloading? When the functions defined are the same, but the unknown contents of the participating operations are different. Then, a function name is defined to represent the function, to facilitate reading, and to distinguish multiple functions with the same name by different parameter lists. */class functionoverload {public static void main (string[] args) {//add (4,5);//add (4,5,6);p rint99 ();} public static void print99 (int num) {for (int x=1; x<=num; x + +) {for (int y=1; y<=x; y++) {System.out.print (y+ "*" +x+ "=" +y*x+ "\ t");} System.out.println ();}} Print 99 multiplication table public static void Print99 () {print99 (9);} Defines an addition operation that gets the sum of two integers. public static int Add (int x,int y) {return x+y;} Defines an addition, which gets the sum of three integers. public static int Add (int x,int y,int z) {return add (x, y) +z;}} /*void Show (int a,char b,double c) {}a.void Show (int x,char y,double z) {}//not, because it is the same as the original function. B.int Show (int a,double C,char b) {}//overloaded because the parameter types are different. Note: It's okay to overload and return value types. C.void Show (int a,double C,char b) {}//overloaded because the parameter types are different. Note: It's okay to overload and return value types. D.boolean Show (int C,char b) {}//is overloaded because the number of parameters is different. E.void Show (double c) {}//overloaded because the number of parameters is different. F.double Show (int x,char y,double z) {}//No, this function cannot be present with a given function in a class. */
Rectangular, 99 table
/*1, defines a feature that is used to print rectangles. 2, define a function that prints the function of the 99 multiplication table. */class functiontest{public static void Main (string[] args) {//draw (5,6);//printhr ();//draw (7,9);//printhr (); Print99 ();} /* Define a function that prints the 99 multiplication table function. */public static void Print99 () {for (int x=1; x<=9; x + +) {for (int y=1; y<=x; y++) {System.out.print (y+ "*" +x+ "=" +y*x+ " \ t ");} System.out.println ();}} /* Defines a feature for printing rectangles. Idea: 1, determine the result: no, because direct printing. So the return value type is VOID2, is there an unknown content? There are, two, because the rows and columns of a rectangle are indeterminate. */public static void Draw (int row,int col) {for (int x=0; x<row; x + +) {for (int y=0; y<col; y++) {System.out.print ("*"); }system.out.println ();}} public static void Printhr () {System.out.println ("------------------------------");}}
Java Object-oriented overview, function parsing