Scene One, there are 100 requirements, output 100 different string content.
=============== demand 1 Output "I'm Chinese" =================//
=============== Demand 2 Output "I love China" =================//
=============== Demand 3 Output "study hard" =================//
=============== Demand 4 output "Day up" =================//
=============== ... "=================//
=============== demand 99 Output "XXXX" =================//
=============== demand 100 Output "XXXX" =================//
"Example code 1:"
Packagelesson15; Public classTest2 {/** * @paramargs*/ Public Static voidMain (string[] args) {//=============== Demand 1 Output "I'm Chinese" =================//System.out.println ("I am Chinese"); //=============== Demand 2 Output "I love China" =================//System.out.println ("I love China"); //=============== Demand 3 output "study hard" =================//SYSTEM.OUT.PRINTLN ("Study hard"); //=============== Demand 4 output "day up" =================//System.out.println ("Day Up"); //=============== demand ... "=================// //=============== demand 99 Output "XXXX" =================// //=============== Demand 100 Output "XXXX" =================// //"Note 1," We found each time is the output character demand, the output content is different, the output code is the same, each time to write once System.out.println ()//There is a lot of duplicate code System.out.println ()//"NOTE 2," If there are 100 demand output strings, we have to write 100 lines System.out.println (). //"Optimization" use method to optimize }}
Use method Optimization:
"Sample Code 2"
Packagelesson15; Public classTest3 {/** * @paramargs*/ Public Static voidMain (string[] args) {//=============== Demand 1 Output "I'm Chinese" =================//println ("I am Chinese"); //=============== Demand 2 Output "I love China" =================//println ("I love China"); //=============== Demand 3 output "study hard" =================//println ("Study hard"); //=============== Demand 4 output "day up" =================//println ("Day Up"); } //define a method to perform the operation of the output Public Static voidprintln (String str) {System.out.println (str); }}
Scenario two, if we want to precede each line of the output of scene one, add the words "inside Empty is:". If it is implemented as "sample code 1", it will be 100 times more cumbersome.
And if we use the "example code 2" above to make changes, it is more convenient. As follows:
Packagelesson15; Public classTest3 {/** * @paramargs*/ Public Static voidMain (string[] args) {//=============== Demand 1 Output "I'm Chinese" =================//println ("I am Chinese"); //=============== Demand 2 Output "I love China" =================//println ("I love China"); //=============== Demand 3 output "study hard" =================//println ("Study hard"); //=============== Demand 4 output "day up" =================//println ("Day Up"); } //define a method to perform the operation of the output Public Static voidprintln (String str) {System.out.println ("The content is:"+str); Use method, easy maintenance}}
Summary: Using the method, reduce a lot of duplicate code, easy to maintain.
The benefits of how methods are used and methods in Java