class Count{
//定义一个实现两个数求和的方法
//int 是返回值类型
public int getSum(int a,int b){
//(1)a=3 b=4 a+b=7
//(1)a=8 b=-5 a+b=3
return a+b;
}
public int getSub(int x,int y){
//(2)x=8 y=-5
// return a-b;
//方法内调用其他方法 getSum(x,-y)=3
return getSum(x,-y);
}
}
public class ClassSumSubDemo {
public static void main(String[] args){
//创建一个实例化对象 用于使用类中的方法
Count c =new Count();
//(1)a 是形参 3是实参
c.getSum(3,4);
System.out.println(c.getSum(3,4));
//(2)将8和5传递给x和y res=3
int res=c.getSub(8,5);
System.out.println("8-5="+res);
}
}
【JAVA_实现两个数求和的方法】
于 2022-09-21 20:53:14 首次发布