Chapter 1 method (1) (2) and chapter 10 Method

Source: Internet
Author: User

Chapter 1 method (1) (2) and chapter 10 Method

1. What is the role of the method?

Make the program shorter, clearer, and easier for program maintenance. It can improve the efficiency of Program Development and code reusability.

2. Four Elements of method definition:

Method return type method name form parameter list and method body.

3. Method Syntax:

Access modifier return value type method name (){

Method subject;

}

4. method name specifications:

1) it must start with a letter, "_", ".", or "$ ".

2) It can have numbers, but cannot start with numbers.

3) if the method name is composed of multiple words, the first letter of the first word is lowercase, And the last letter is uppercase.

4) verbs are used for method names.

5. return values of methods

1) The method does not return a value. The return type is void.

Example: public void showInfo (){}

2) The method has a return value. You must inform the type of the return value. You must use the class keyword return to return the value. The return type is the type of the return value.

Example: public String getName (){

Return "Zhang San ";

}

Note: return cannot be returned for void.

6. Common Errors

1) The return type does not match

2) The main method cannot return values.

3) A method can have at most one return value.

7. Call Method

1) in different classes: Object Name. Method Name ();

2) In the same type: Method Name ();

8. Method Classification

Methods can be divided into constructor methods and custom methods based on the parameter types of methods.

1) The syntax for defining methods without parameters is:

Public return value type method name (){}

2) syntax for defining methods with parameters:

Public return value type method name (form parameter list ){}

9. Formal parameters and actual parameters

"Formal Parameters" are the parameters used when the method name and method body are defined. They are used to call the parameters passed in when the method is called. It is not a real variable, so it is also called a virtual variable.

"Actual Parameters" are the variables or objects that have been defined and assigned the initial values. They are actually existing data, also known as the number of actually involved operations.

10. constructor:

The constructor must meet the following conditions:

1) The method name must exactly match the class name

2) do not declare the return value

3) static final abstract native cannot be modified, and return values cannot be returned.

11. When using the new keyword to create an object, pay attention to three aspects

1) allocate memory space for the object instance

2) Call the constructor

3) return the reference of the object instance

Note: differences between constructor and constructor

The constructor is executed only when the JVM implements the creation of class instances. The constructor usually contains the field initialization code. Strictly speaking, the constructor is not a method because it does not return any value.

12. The constructor can be divided into three types based on different parameters.

1) Implicit construction method without parameters (Default Construction Method)

2) display the construction method without Parameters

Example:

Public Person (){

System. out. println ();

}

3) display the construction method with Parameters

Example:

Public Person (int age ){

System. out. println ();

}

13. Method Overloading

A class can define multiple methods with the same name but different parameter lists (parameter types, numbers, and sequences, the corresponding method is selected based on different parameter lists.

14. Scope

The position of the variable declaration determines the scope of the variable.

The variable declared in the method is called a local variable: the scope is in the current method.

The variables declared in the class are called member variables, such as attributes. The scope is the whole class.

15. this keyword

This keyword can only be used inside a method, indicating a reference to the object that calls the method. If you call another method in the same class, you do not need this keyword, you can call it directly.

16. this keyword has the following usage:

1) when the parameter name of a non-static method in the class is the same as a member variable name of the class, in order to prevent the parameter from covering the scope of the member variable, you must use the this keyword explicitly.

2) If the first statement of a constructor has the form of this (...), this constructor will call other constructor methods in this class.

3) if a method needs to pass in the current object, it can pass the current object as a parameter.

17. Package naming rules

The website of the company is written in reverse form, followed by the project name, and the name of the file to which the class belongs

18. Functions of packages

1) allows classes to form small units (similar to folders), which makes it easy to find and use corresponding files.

2) better data protection and Methods

3) Prevent name conflicts

 

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.