javaSE基础03之方法

3.1 方法的重载  

方法重载:方法名称相同,但是参数的类型或个数不同

public class Demo01 {

    /**
     * @param args the command line arguments
     */
     int add(int a,int b){
         System.out.print("方法一:");
         return a+b;
     }
    int add(int a, int b,int c){
        System.out.print("方法二:");
        return a+b+c;
    }
    int add(int a,String b){
        System.out.print("方法三:");
        return a+Integer.parseInt(b);
    }
     
    public static void main(String[] args) {
        // TODO code application logic here
       Demo01 demo=new Demo01();
       System.out.println(demo.add(3, 4));
       System.out.println(demo.add(3, 4,5));
       System.out.println(demo.add(3, "4"));
    }
    
}

3.2static静态方法

Static方法属于类本身;调用方法:1.类名.方法;2.对象.方法

普通方法:方法属于类的对象;调用方法:1,对象.方法

public class Demo02 {

    /**
     * @param args the command line arguments
     */
    void fun01(){
        System.out.println("这是一个普通方法");
    }
    static void fun02(){
        System.out.println("这是一个静态方法");
    }
    
    @SuppressWarnings("static-access")
    public static void main(String[] args) {
        // TODO code application logic here
        
        Demo02 demo=new Demo02();
        demo.fun01();
        Demo02.fun02();
        demo.fun02();
    }
    
}

3.3 递归方法

方法同过直接或间接的方式自己调用自己

public class Demo03 {

    /**
     * @param args the command line arguments
     */
    
    static long notDiGui(int n){
        long result=1;
        for(int i=1;i<=n;i++){
            result=result*i;
        }
        return result;
    }
    
    static long diGui(int n){
        if(n==1){
            return 1;
        }else
        return n*diGui(n-1);
    }
    //斐波那契数列
    static int fibonacci(int n){
        if(n==1 || n==2){
            return 1;
        }else
        return fibonacci(n-2)+fibonacci(n-1);
    }
    public static void main(String[] args) {
        // TODO code application logic here
        System.out.println(Demo03.notDiGui(5));
        System.out.println(Demo03.diGui(5));
        System.out.println(Demo03.fibonacci(6));
    }
    
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值