Files
javaSE251223/day04/src/com/inmind/Demo01_method.java

50 lines
1.5 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package com.inmind;
/*
方法_定义格式的详细说明
修饰符 返回值类型 方法名(){
方法体;(return只跟方法有关)
}
a.修饰符public static (固定的)
b.返回值类型:
1.没有返回值void
2.有返回值基本数据类4类8种java中的任意类型都能写
c.方法名:自定义即可,在方法被调用时进行使用
d.():方法参数(参数列表)
1.如果没有参数,就直接()即可
2.如果有参数,直接在()中定义
2个int型参数(int a,int b)
e.{}:方法体就是java语句的集合
1.在方法中是可以使用关键字return,return就是结束当前的方法遇到return方法就结束了
return 可以将之后的值返回到方法调用处。
2.如果方法有返回值就必须在方法体中输入return 返回值类型的数据;
3.如果方法没有返回值就可以不写但是也可以写成return;
方法定义的2个明确
1.明确返回值类型
2.明确参数列表
案例定义方法实现2个整数值的相加之和并返回
*/
public class Demo01_method {
public static void main(String[] args) {
//调用加法功能,实现整数相加得结果
int sum = addMethod(10, 20);
System.out.println(sum);
}
//2个整数值的相加之和并返回
public static int addMethod(int a,int b) {
int result = a+b;
return result;
}
}