day04--方法_定义格式的详细说明
This commit is contained in:
@@ -0,0 +1,11 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<module type="JAVA_MODULE" version="4">
|
||||||
|
<component name="NewModuleRootManager" inherit-compiler-output="true">
|
||||||
|
<exclude-output />
|
||||||
|
<content url="file://$MODULE_DIR$">
|
||||||
|
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
|
||||||
|
</content>
|
||||||
|
<orderEntry type="jdk" jdkName="17" jdkType="JavaSDK" />
|
||||||
|
<orderEntry type="sourceFolder" forTests="false" />
|
||||||
|
</component>
|
||||||
|
</module>
|
||||||
@@ -0,0 +1,41 @@
|
|||||||
|
package com.inmind.method01;
|
||||||
|
/*
|
||||||
|
方法的定义格式
|
||||||
|
修饰符 返回值类型 方法名(参数列表){
|
||||||
|
方法体;
|
||||||
|
}
|
||||||
|
|
||||||
|
a. 修饰符 public static (固定)
|
||||||
|
b. 返回值类型:
|
||||||
|
没有返回值:void
|
||||||
|
有返回值:基本数据类型(4类型八种)
|
||||||
|
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 x = 10;
|
||||||
|
int y = 20;
|
||||||
|
int sum = getSum(x,y);
|
||||||
|
System.out.println("求和的结果为:"+sum);
|
||||||
|
System.out.println("程序结束");
|
||||||
|
}
|
||||||
|
//案例:定义一个方法,实现2个整数值的相加操作,并将和值返回
|
||||||
|
public static int getSum(int a,int b){
|
||||||
|
int sum = a+b;
|
||||||
|
return sum;
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user