方法
public class Demond01 {
public static int add(int a,int b){
return a+b;
}
public static void main(String[] args) {
int sum=add(1,2);
System.out.println(sum);
}
}
public static修饰符
void 不需要返回值(return)
static类变量
结构
修饰符 返回值类型 方法名(参数类型 参数名){
。。。。。
方法体
。。。。。
return 返回值;
}
方法重载
方法名可以相同,参数不能相同
可变参数
1.
public class Demo01 {
public void test(int x){
System.out.println(x[3]);
}
public static void main(String[] args) {
Demo01 z=new Demo01();
z.test(1,2,56,47,96,23,32);
}
}
递归(注意:数小的时候用,能不用则不用)
-
自己调用自己(可以减少代码量 eg:阶乘)
-
Demo:
public class Demo01 { public static int test(int n){ if (n==1){ return 1; }else { return n*test(n-1); } } public static void main(String[] args) { //5的阶乘 System.out.println(test(5)); } }