The design of the method of the Java Language Foundation

Source: Internet
Author: User

One of the following principles of development:

Dry principle: don ' t Repeat yourself (don't repeat your own code)

Cause: Repetition means an increase in maintenance costs

     Public Static void Main (string[] args) {        System.out.println ("snippet a");        DoWork ();        System.out.println ("code snippet B");        DoWork ();        System.out.println ("code snippet C");        DoWork ();        System.out.println ("code snippet D");        DoWork ();    }     // pulling the common code out of the method    Static void doWork () {        System.out.println ("Common 200 lines of code");    }

Method defines the format:

[modifier] Returns a value type method name (formal parameter 1, formal parameter) {

Method body

(If the method needs to return a result to the caller, a return value is required)

}

Terminology in the method:

  modifiers : public, static and so on, static modified methods belong to the class, directly using the class name can be called , the common method represents a method, the object can be called by the class;

return value type : The method is actually to complete a function, after the function is finished, whether you need to return a result to the caller

If you do not need to return the result to the caller, declare it using the keyword void, meaning that there is no return result ;

  Method Name : Follow the identifier specification, the use of verbs , the first letter lowercase, if it consists of multiple words, using hump notation, followed by the first letter of each word capitalized;

  Formal Parameters : The method of the variable in parentheses, is only a placeholder, the name of the formal parameter actually does not matter, and the formal parameters can have more than one;

  parameter list : parameter list = = parameter type + parameter number + parameter order ;

  method Signature : Method signature = = Method name + method parameter list ;

(in the same class, the method signature is unique, otherwise the compilation error)

  method Body : The code in the {} of the method that represents the code that completes the function;

  return value : Inside the method, use the return keyword:

Function One: Returns a result value to the caller , at which point the method cannot use void decoration .

Function Two: end the current method .

  actual parameter : The value of the parameter actually passed by the caller when invoking a specific method .

Method Caller: Where to invoke a method, then where is the caller of the method

 Public Static void Main (string[] args) {    //TODO}
The main method is specifically called by the Main method, and we just start the JVM

Location of the method definition:

1): defined in the class, the smallest program unit in Java is the class;

2): Method definition In addition to other methods, the relationship between methods and methods is sibling

3): The sequence of method definitions does not affect

How to define/design methods:

1): Whether the return value type needs to be defined;

2): Whether formal parameters are required

 3): Specific needs to define what functional methods;

-------------------------------------------------------------------------------

1): Whether the return value type needs to be defined:

  We say that the method is to complete a function after the completion of the function, whether you need to return a result data to the caller, if you do not need to return the result data, at this time using the Void declaration (no return)

If you need to return a result data, we will use the result data type as the return value type of the method,

The exact action is print: At this point we care about the process of executing the method, not the result, so use void declaration

The operation is for the sum of two numbers: at this time we care about the method of execution of the process, and after execution, we need to give the caller a feedback

2): Whether formal parameters are required:

  This method in the process of completion of this function, whether there are unknown factors involved, if invited to pass as a parameter, if there is no formal parameter

  For the sum of two numbers, how many of these two numbers, in fact, is unknown to the method, just the caller knows, and different callers pass different parameter values.

Overloaded design of the method

Requirement 1: In the same class, the method that defines the sum of two integers and decimals is defined separately

    // Solution:     // Sum of two integers    Static int getSum1 (intint  y) {        return x + y;    }         // ask for the sum    of two decimals Static double getSum2 (doubledouble  y) {        return x + y;    } 

Requirement 2: The method of printing the String,double,boolean type separately in the same class

    // Solution:     // Print string    Static void pstring (String data) {        System.out.println (data);    }     // Print int    Static void pInt (int  data) {        System.out.println (data);    }     // Print a Boolean    Static void Pboolean (boolean  data) {        System.out.println (data);    }

Overloaded design of Methods (overload): overloaded methods are defined in the same class, where a method allows more than one method of the same name, as long as their argument lists are different.

Method overloading: Methods that mask the same function are different due to different parameters

method overload judgment Principle :"Two same different"

   two same: The method name is the same in the same class;

A difference: The method parameter list is different ( parameter type, parameter number, parameter order );

A different parameter list is different as long as the parameter type parameter has a parameter order.

Note: method overloads are independent of the return value type of the method , except that the return value type is generally required to be consistent.

     parameter list and parameter names it doesn't matter, the overloads of the method and the parameters don't matter.

The design of the method of the Java Language Foundation

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.