Java Basics finishing [4] (method)

method

java not distinguish between functions and methods, it is to function

What is the method?

To solve the matter cycle is repeated operation, under some circumstances, some of the code requires a lot of repeated cycles can not meet the requirements at this time, then the code can be encapsulated, the encapsulation method is

The method is repeated calls for a code segment, to be used multiple times in multiple places this code, if repeated write this code in various places, it certainly would be more trouble, and if this part of the code to be modified, too much trouble, so at this time, this code can be defined as a method, a program for repeated use.

方法格式:  
        访问权限修饰符[其他修饰符] 返回值类型 方法名(参数列表){
                           方法体; 
                          return;   
          }

When the method defined between the method and the method is same level relationship
method Detail Description :
Access modifiers:
Provides the current method can be invoked by whom (who accesses)
access modifier There are four: public common maximum authority, no one can be accessed

Other modifiers:
other modifiers enclosed in brackets, is can be omitted in
other modifiers four kinds: static static belong to the class

Return Value Type:
Results after the current method to complete a function, what can be
1, no return type - >> void This method does not return any results obtained
2, there Return Value Type: Value type (basic data types) reference types (array, interface, set, flow, etc.)

Method name:
the method name to comply with express symbol naming convention to make use of the verb
large hump: all letters capitalized small hump: the first word lowercase, follow the first letter of each word capitalized example: getNumber ------- >>> hump nomenclature

List of parameters (parameter list):
parameter to be provided to the method, to obtain an external passed in the parameter
list of parameters: the data type of parameter name, the data type parameter name
parameter list is defined variable which no specific value, as long as the method is not called, this variable will not have any space

Method signature:
Method Each method signature is unique, if the same error signature appears will compile
the composition of the method name parameter list +

 return关键字:
 是在方法体内部使用,return关键字可以终止方法或者带回一个返回值
 第一种使用方式无返回值类型:
 此时方法中可以使用return也可以不写return,但是绝对不能在return后面添加
 任何具体的值。
 第二种使用方式有返回值类型:
 此时方法必须使用return,必须带有一个当前返回值类型一致的值。
 ps:正常来说方法中只有一个return,即执行一个return操作!
 有多个return时,只能执行一个return,其他的用分支判断隔开

Call the method of
writing within the method body, static method call in two ways:

第一种:   方法名();

ps: if the current process has a parameter list, the data corresponding to dislike constant, variable or expression, or other methods must be passed in, the type and number of incoming data must be consistent with the data type and the number of the parameter list.
Incoming data argument - >>> actual parameters (specific value)
// if the current method returns a value, the option to accept may choose not to accept, particularly the need to see if the return value of the succeeding code

第二种调用方式:通过类名.静态方法名(); 静态属于类,类可以调用静态方法

Two different points: in the same class static methods can not use the class name invoke, in other classes need access to the current class hate static method, you must invoke the name of the class method ps: MethodDemo.printNineNine ();..

No return type of the method must unacceptable ps: printNineNine ();

Summary: The method call is divided into homogeneous and heterogeneous, and there is no return value and return value;
similar call directly write * method name (corresponding to the parameter list) * Call
call different class method name where the class name method name (correspondence. parameter list) to call
ps: how to determine if a method returns a value system.out.println (method name ());

Common Styles method

public class MethodDemo2{
    public static void main(String[] args){
    }
    }
	//1.无参无返回值 --> 纯打印
  public static void show1(){ }
    //2.有参无返回值 --> 打印
    public static void show2(int a){ } //3.有参有返回值 --> 计算
    public static int show3(int a){ } //必须有return //4.无参有返回值--> 计算
    public static int show(){ } // 必须有return
}

Method ring memory
requirements: define a method, and the sum of two numbers
Here Insert Picture Description

Overloaded methods
learning method, it is the indirect learning object oriented -> package, also polymorphic in the learning
method polymorphic ---- >>>
same functionality of the method, no need to define a method different names, this is not conducive memory! The name of the same functionality of the method, by the same method name, parameters may achieve different hate overloaded. Just remember the name of a method, depending on the incoming data, the system automatically selects the most appropriate method hate

Defined overloaded:
method signature parameter list = name + method of
method overloading method name that is the same, but different list of parameters can only meet one of the following, that is overloading
a different data type parameter hate different number 2 3 different order
as long as the method name and parameter list of requirements can be, regardless of all the other
benefits: You can do a lot of recording method name, you can call the method name, we hate to decide to implement dynamic logic through a method parameter

系统会通过程序员传入的参数来动态决定调用哪个方法,所以会出现一个重载的中的问题(避免出现下面的问题)
 public class OverloadDemo2 {
        public static void main(String[] args) {
			//求和
				byte num1 = 1;
				byte num2 = 1;
				//OverloadDemo2 中的方法 add(byte,short) //OverloadDemo2 中的方法 add(short,byte) 都匹配 //二义性
                 int sum = add(num1,num2); System.out.println(sum);
           }
            public static int add(byte num1 ,short num2){
            return  num1+num2;
    }
    public static int add(short num1,byte num2){
} }

Recursive (primary)
recursive method itself is calling itself, is very serious for memory consumption
recursion itself is equivalent to the method body inside the hate there is an implicit cycle is constantly hate calls itself (per se)
recursively no outlet that is not the end point, At this JVM will throw a stack overflow error StackOverFlowError

Here Insert Picture Description

Guess you like

Origin blog.csdn.net/root1994/article/details/88911133