The Declaration form and function of the method
Return value data type method name (formal parameter list)
{
Execute the statement;
return value;
}
---method is the behavior of an object
---method is to define a separate applet that has a specific function in the class
---method is also called a function.
Method overloading
--in Java, if you have multiple methods with the same name but different parameters, it becomes the overloaded method
Three main principles of method overloading
-Method name is the same
-Different parameters (can be different in three ways)
--Different quantity
--Different types
--Different order
-Same scope
Only methods that have different return values do not form overloads
Instance:
public class Overloadtest {
Define a method to add a two integer
public void Add (int a,int b) {
int sum = a+b;
System.out.println ("The sum of two integers is:" + (a+b));
}
Define a method to add a three integer
public int Add (int a,int b,int c) {
return a+b+c;
}
Define a method to add two floating-point numbers
Protected double Add (double a,double b) {
return a+b;
}
Define a method to add an integer number and a decimal
void Add (int a,double b) {
Double sum = a+b;
System.out.println ("The sum of two integers is:" +sum);
}
Declaration form and function of construction method
Constructor method: Used to instantiate a class, that is, to create an object.
Composition of the construction method
Access permission modifier class name (formal argument list) {method body}
--Called when the object is instantiated.
-No return value, not even void
--the method name must be the same as the class name
--cannot use modifiers, including static, final, abstract
Java object creation and use
--Call a constructor without parameters
Class Name Object name = new constructor method for this class ();
Example: Book Javabook = new book ();
--Call the constructor method with parameters
Class Name Object name = new constructor method for this class (parameter 1, parameter 2 ...);
Example: Book Javabook = new book (1, "Journey to the Monkey");
Working with objects: using "." Do the following
---Access the properties of the class: Object name. Properties
---method of calling the class: Object name. Method Name ()
The use of java-method and construction method