Definition of the method, as well as heavy duty calls

And defined method call overloading

definition:

public  static  void method name () {
 // method body; 
}

Demo:

// Demo 
public  static  void Method () {
 // method body; 
}

transfer:

Method name ();
method();

Note: The method need to define, in the call, otherwise the program will be given, showing no void return value, return can be omitted, also be individually written return, back without data, the method can not be nested

None None Return Value Method Reference:

public void methed(){
}

There reference method return value:

public void methed(int a){
}

There are no parameters Return:

public int methed(){
}

There are parameters that return values:

public int methed(int a){
}

Note: When the definition method, a plurality of parameters using a comma (,) separated

Exercise: Demand: design a method to obtain the higher of the two numbers, data from the parameter

public  class MethodTest {
 public  static  void main (String [] args) {
 // the main () method calls the method defined and stored using the variable 
int Result = getMax (10,20 );
System.out.println(result);
// call the method defined in main () method and print the result 
System.out.println (getMax (10, 20 ));
}
// definition of a method for obtaining a larger number of the two numbers 
public  static  int getMax ( int A, int B) {
 // use branching statements two cases the size of the relationship of the two numbers are processed
 // The returns the corresponding title set are respectively disposed in both cases results 
IF (a> B) {
 return a;
} else {
return b;
}
}
}

Arguments and parameters of the method parameters;

Related to the value of the transfer and delivery address. Value is passed, it does not affect the value, addressing transport in value, with the JVM works related to the following show you

Value is passed:

public class Demo {
    public static void main(String[] args) {
        int a = 10;
        Test(a);
        System.out.println(a);

    }
    public static void Test(int a){
        a = 12;
    }
}

result:

Address delivery:

public class Demo {
    public static void main(String[] args) {
        int [] arr = {10,20};
        Test(arr);
        System.out.println(arr[0]);

    }
    public static void Test(int [] arr){
        arr[0] = 20;
    }
}

result:

Value passed by the above described test object is not changed, changing the value of the object transfer site

Overload refers to a method of the relationship between a plurality of the same methods defined in the class, the following conditions each constituted of a plurality method overloading

  A plurality of methods in the same class in
  the plurality of methods with the same method name
  parameters of a plurality of different methods, different types or different numbers of note that: corresponds to only reload call definition method, and the method, with reference to the standard format is called
  overload only for identifying the name of the parameter class of the method of the same, regardless of the return value, in other words can not be two return value to determine
  whether a method constituting another reload

Example:

public  class MethodDemo {
 public  static  void Fn ( int A) {
 // method body 
}
 public  static  int Fn ( Double A) {
 // Method member 
}
}
public  class MethodDemo {
 public  static  a float Fn ( int A) {
 // method body 
}
 public  static  int Fn ( int A, int B) {
 // Method member 
}
}

Exercise:

Requirements: Use heavy-duty thinking, design comparing two integers are the same way, compatible with the full integer types (byte, short, int, long)

public class MethodTest {
public static void main(String[] args) {
//调用方法
System.out.println(compare(10, 20));
System.out.println(compare((byte) 10, (byte) 20));
System.out.println(compare((short) 10, (short) 20));
System.out.println(compare(10L, 20L));
}
//int
public static boolean compare(int a, int b) {
System.out.println("int");
return a == b;
}
//byte
public static boolean compare(byte a, byte b) {
System.out.println("byte");
return a == b;
}
//short
public static boolean compare(short a, short b) {
System.out.println("short");
return a == b;
}
//long
public static boolean compare(long a, long b) {
System.out.println("long");
return a == b;
}
}

 

Guess you like

Origin www.cnblogs.com/robinson-coding/p/12525324.html