方法
代码
package com.frey.method;
public class Demo01 {
//main方法
public static void main(String[] args) {
int sum=add(1, 2);
System.out.println(sum);
System.out.println("====================");
int max=max(1, 2);
System.out.println(max);
}
public static int add(int a, int b) {
return a+b;
}
public static int max(int a,int b) {
if (a==b)
{
System.out.println("两数相等");
return 0;
}
int result=-1;
if (a<b)
{
result=b;
}
else
{
result=a;
}
return result;
}
}
方法的重载
- 课堂截图
![picture](https://img2020.cnblogs.com/blog/2657293/202112/2657293-20211208204122526-565621409.jpg)
public static int max(int a,int b) {
if (a==b)
{
System.out.println("两数相等");
return 0;
}
int result=-1;
if (a<b)
{
result=b;
}
else
{
result=a;
}
return result;
}
public static double max(double a,double b) {
if (a==b)
{
System.out.println("两数相等");
return 0;
}
int result=-1;
if (a<b)
{
result=b;
}
else
{
result=a;
}
return result;
}