The use of java-method and construction method

Source: Internet
Author: User

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

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.