JAVA之方法的重载,递归与覆写

1.方法的重载(Overload)

定义:方法名相同,参数不同(参数类型或者个数不同)
方法的返回值类型不影响重载
有时候我们需要用到一个函数兼容多种参数的情况
这时候就要用到方法的重载
使用代码:

public class Exercise {
    public static void main(String[] args) {
        int a = 10;
        int b = 20;
        int ret = add(a, b);
        System.out.println("ret= " +ret);
        double a2 = 10.3;
        double b2 = 20.5;
        double ret2 = add(a2,b2);
        System.out.println("ret2= "+ret2);
        double a3 = 10;
        double b3 = 20;
        double c3 = 30;
        double ret3 = add(a3, b3, c3);
        System.out.println("ret3= " +ret3);

    }
        public static int add(int x,int y){
            return x+y;
        }
        public static double add(double x,double y){
             return x+y;
        }
        public static double add(double x,double y,double z){
            return x+y+z;
        }
}

同一个方法名字,提供不同的版本实现,叫做方法重载

2方法的递归

一个方法在执行的过程中调用自身,就叫做递归
例如:递归求n的阶乘

public class Multiple {
    public static void main(String[] args) {
        int n=6;
        int ret=factor(n);
        System.out.println("ret= "+ret);
    }
    public static int factor(int n){
        if(n==1){
            return 1;
        }else{
            return n*factor(n-1);//函数自身的调用
        }
    }
}

练习:求斐波那契数的第40个数

public class Word{
    public static int count=0;
    public static void main(String[] args) {
        System.out.println(fib(40));
        System.out.println(count);
    }
    public static int fib(int n){
        if(n==1||n==2){
            return 1;
        }
        if(n==3){
            count++;
        }
        return fib(n-1)+fib(n-2);
    }
}

3.方法的覆写(Override)

定义:子类定义了与父类方法名称,参数个数及返回值类型相同的方法。但是被覆写不能够拥有比父类更为严格的访问权限
简单代码:

public class Person {
    public void print(){
        System.out.println("1.[Person]类的print方法");
    }
}
class Student extends Person{
    public void print(){
        System.out.println("2.[Student]类的print方法");
    }
}
    public class Test {
        public static void main(String[] args) {
            new Student().print();
        }
    }

在这里插入图片描述

注意:
1.你当前使用的对象是通过哪个类new的
2.当调用某个方法时,如果该方法已经被子类覆写了,则调用的一定是被覆写过的方法
要求:被覆写的方法不能有比父类更为严格的访问权限
访问权限:public>default>private 意味着如果父类使用public进行声明,那么子类也必须使用public声明,如果父类使用default声明,那么子类使用default或者public声明
错误的覆写

class Person {
    public void print(){
        System.out.println("1.[Person]类的print方法");
    }
}
  class Student extends Person{
     void print(){
        System.out.println("2.[Student]类的print方法");
    }
}
public class Test1 {
    public static void main(String[] args) {
        new Student().print();
    }
}

以后写方法的时候,%99.99的情况下建议使用public;
在写属性的时候,建议使用private

问题:如果现在父类中使用private定义方法,子类使用public定义方法对吗?

代码:

class Person{
    public void fun(){
        this.print();
    }
    //如果现在父类使用了private定义方法,那么表示该方法只能被父类使用
    //也就也意味着子类根本不知道父类有这样的方法
    private void print(){
        System.out.println("1.[Perosn]类的print方法");
    }
}
class Student extends Person{
    //此时的方法只是子类定义的新方法,和父类并没有什么关系
    public void print(){
        System.out.println("2.[Student]类的print方法");
    }
}
public class Test1{
    public static void main(String[] args) {
        new Student().fun();
    }
}

运行结果:1.[Person]类的print方法

重点

:覆写(override)与重载(overload)的比较
1)概念:重载是指方法名相同,参数的类型及个数不同
覆写参数的方法名,参数类型及个数,返回值类型完全相同
2)范围:重载 一个类
覆写 继承关系
3)限制:重载 没有限制关系
覆写 被覆写的方法不能拥有比父类更为严格的访问权限

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值