day04-方法_定义格式的详细说明
This commit is contained in:
42
day04/src/com/inmind/method01/Demo01.java
Normal file
42
day04/src/com/inmind/method01/Demo01.java
Normal file
@@ -0,0 +1,42 @@
|
||||
package com.inmind.method01;
|
||||
/*
|
||||
方法的定义格式:
|
||||
修饰符 返回值类型 方法名(参数列表){
|
||||
方法体;
|
||||
}
|
||||
|
||||
a. 修饰符 public static (固定)
|
||||
b. 返回值类型:
|
||||
没有返回值:void
|
||||
有返回值:基本数据类型(4类8种) 引用数据类型还没学暂时不用
|
||||
c.方法名:标识符(硬性要求,软性建议小驼峰)
|
||||
d.():参数列表
|
||||
如果没有参数()
|
||||
如果有参数,直接在()定义
|
||||
2个int参数(int a,int b)
|
||||
e.方法体:java代码的集合{}
|
||||
在方法中可以使用关键return,就是结束当前方法,并且,如果有返回值,直接在
|
||||
return之后 编写。将return后的值,返回到方法调用处。
|
||||
|
||||
方法定义的2个明确:
|
||||
1.明确返回值类型
|
||||
2.明确参数列表
|
||||
|
||||
案例:定义一个方法,实现2个整数值的相加操作,并将和值返回
|
||||
*/
|
||||
public class Demo01 {
|
||||
public static void main(String[] args) {
|
||||
//调用已经定义好的方法
|
||||
int a = 10;
|
||||
int b = 20;
|
||||
int sum = getSum(a,b);
|
||||
System.out.println(sum);
|
||||
}
|
||||
|
||||
//案例:定义一个方法,实现2个整数值的相加操作,并将和值返回
|
||||
public static int getSum(int a,int b){
|
||||
int sum = a + b;
|
||||
return sum;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user