The construction method and common method java

1. The common method:

Definition: The method is simply to complete the block of a particular function.

Common method definition format:

Modifier return type method name (parameter type parameter name 1, parameter type parameter name 2, .........)

{

            Function thereof;

            return Return value;

}

Return type for defining the type of data return value.

Common method is divided into: there is a clear method return value and there is no clear method return values.

(1) there is a clear method returns the value of the call there (called separately (meaningless), the output call, the assignment calls (recommended))

public static int sum (int a , int b)
{
    int c =a+b;
    return c ;
}
public static void main(String[] args)
{
           // sum (10,20); // call alone
           //System.out.println(sum(10,20)); // output call
           int sum01 = sum (10,20); // assignment calls
}

  

(2) there is no clear method returns the value of calls (call void type method)

It can only be used alone.

2. Method configuration (Definition: Simple is initialized with the data objects.)

Constructor is a special member of the class, it will be called automatically when the class is instantiated.

Constructor defined format:

Constructor defined format:

Modifier method name ()

{

}

  

Note: a class name and method name of the same.

           2. There is no return type.

           3. No specific return value.

Constructor divided into: no constructor ginseng and ginseng have constructor

(1) constructor with no arguments

public class Student{

public Student () {// constructor with no arguments

System.out.println ( "method is called without parameter");

}

}

public class Test {

public  static void main (String[]  args){

Student stu = new Student (); // instantiate objects Student

}

} // This method is the constructor with no arguments, i.e. constructor does not receive parameters, the output can be performed, the constructor is called with no arguments.

  (2) Parameter constructor

A property assignment in two ways: ①, direct access object properties ②, methods setXxx () 
If you would like to assign a value for the instance of the object while the object's properties can be achieved by the constructor arguments

public class Student{

private String name;

private int  age;

public Student (String name, int age) {// constructor parameters have

    this name = name;

    this  age  = age ;

}

public  void show (){

System.out.println ( "Name:" + name + "age-:" + age);

}

}

public  class  Test02(){

public static void main (String[]   args)

{

Student stu = new Student ( "Wang five", 25); // instantiate an object assigned at the same time

stu.show();

}

}

  

Constructor Note:

Each class 1.java have at least one constructor, if we did not create, the system creates a default constructor with no arguments for us. (No code displayed)

2. However, if we define a constructor method, the system will no longer provide a default constructor with no arguments for us. This time want to use a non-constructor parameter must own definition. Because when instantiating an object, not necessarily the input parameters, then there is no no-argument constructor, not an input parameter, an error occurs.

3. The method of construction need to modify the public, the use of private, can not be instantiated in the other class.

4. The method of construction is overloaded.

Guess you like

Origin www.cnblogs.com/sgy614092725/p/shiguiyu20.html