正文
1.1不带参数的方法调用
| 定义格式: |
| public static void 方法名(){ |
| 方法体; |
| } |
| |
| 调用格式: |
| 方法名(); |
| Method01.java |
| public class Method01 { |
| public static void main(String[] args) { |
| Method01.isEvenNum(); |
| } |
| |
| |
| public static void isEvenNum(){ |
| int num = 10; |
| num = 9; |
| if(num % 2 == 0) { |
| System.out.println("是偶数"); |
| } else { |
| System.out.println("不是偶数"); |
| } |
| } |
| } |
1.1.1 不带参数的方法调用示例
| |
| |
| |
| |
| |
| |
| |
| public class Method02 { |
| public static void main(String[] args) { |
| getMax(); |
| } |
| public static void getMax(){ |
| int a = 29; |
| int b = 200; |
| if(a > b) { |
| System.out.println(a + "大"); |
| } else{ |
| System.out.println(b + "大"); |
| } |
| } |
| } |
1.2带参数的方法定义和调用
1.2.1带参数方法定义

1.2.2带参数方法定义

1.3带参数调用练习
1.3.1带参数调用练习1
| |
| |
| |
| |
| |
| |
| public class Method01 { |
| public static void main(String[] args) { |
| |
| |
| |
| |
| |
| int c = 10; |
| isEvennumber(c); |
| } |
| |
| |
| public static void isEvennumber(int a) { |
| if(a % 2 == 0) { |
| System.out.println(a + "是偶数"); |
| } else { |
| System.out.println(a + "不是偶数"); |
| } |
| } |
| } |
1.3.2带参数练习2
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| public class MethodTest { |
| public static void main(String[] args) { |
| |
| |
| |
| |
| int c = 100; |
| int d = 200; |
| getMax(c,d); |
| } |
| |
| public static void getMax(int a, int b){ |
| if(a > b){ |
| System.out.println(a + "大"); |
| } else { |
| System.out.println(b + "大"); |
| } |
| } |
| } |
1.4带有返回值方法定义和调用
1.4.1带有返回值方法定义

1.4.2带有返回值调用

1.4.3带有返回值方法定义和调用示例

| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| public class MethodDemo { |
| public static void main(String[] args) { |
| |
| |
| isEvenNumber(2); |
| |
| boolean res = isEvenNumber(2); |
| |
| System.out.println(res); |
| } |
| |
| |
| public static boolean isEvenNumber(int num) { |
| if(num % 2 == 0) { |
| return true; |
| } else { |
| return false; |
| } |
| } |
| } |
1.4.4带有返回值方法调用定义练习
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| public class Method01 { |
| public static void main(String[] args){ |
| |
| |
| |
| |
| |
| System.out.println(getMax(1000,200)); |
| } |
| |
| public static int getMax(int x,int y){ |
| if(x > y){ |
| return x; |
| } else { |
| return y; |
| } |
| } |
| } |
1.5方法重载
- 1.什么时候使用方法重载
功能相同的时候,尽可以让方法名形同
- 2.什么条件满足了之后构成方法重载
在同一个类中,方法名字相同,参数列表不同
<1>参数数量不同
<2>参数顺序不同
<3>参数类型不同
- 3.方法重载和什么有关
方法重载和方法名有关,和其他无的关
1.5.1方法重载示例
| public class MethedLoder03 { |
| public static void main(String[] args) { |
| m1(); |
| m1(111); |
| m2(1,2.0); |
| m2(2.0,1); |
| m3(3); |
| m3(33.22); |
| } |
| |
| public static void m1() {} |
| public static void m1(int a) {} |
| |
| |
| public static void m2(int a,double b) {} |
| public static void m2(double a,int b) {} |
| |
| |
| public static void m3(int x) {} |
| public static void m3(double x) {} |
| } |
1.5.2方法重载示例2

1.6方法通用格式

· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 使用C#创建一个MCP客户端
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 按钮权限的设计及实现