解析java中方法的重载和重写之间的区别与联系

解析java中方法的重载和重写之间的区别与联系

1 不同点

1.1 方法重写是在不同的类中(父子类),方法重载是在同一类中

1.2 方法重载最初的目的是构造方法的多样化,方法重写的目的是让重写的方法满足子类的需求

1.3 方法的重载要求同名不同参(形参个数、形参顺序、形参类型不同),方法的重写要求同名同参(形参个数、形参属性、形参类型)

2 相同点

2.1 方法的重写和重载都是为了方法而服务的

2.2 无论是方法的重写还是重载,都与形参列表中的形参名字无关

3.典型应用

3.1 方法的重写

3.1.1 toString()方法

目的:用来输出类中的基本信息,没有显示继承父类的话,默认是继承java.lang.Object类,其中toString()是父类Object已经存在了的方法

3.1.2 示例代码
Cat类
public class Cat {
    private String  name;
    private String type;
    private int age;
    public Cat(){
    }
    //定义有参构造,方便实例化对象时,初始化值
    public Cat(String name, String type, int age) {
        this.name = name;
        this.type = type;
        this.age=age;
    }

    public String getName() {

        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
    //type(品种)属性只可以读取,不可以写入
    public String getType() {
        return type;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;

    }

    @Override
    public String toString() {
       return "猫的名字为: "+name+",猫的种类为: "+type+",猫的年龄为: "+age;
    }
}

TestCat类
public class TestCat {
    public static void main(String[] args) {
        Cat cat=new Cat("叮当","狸花猫",8);
        System.out.println(cat.toString());
    }
}

3.1.3 示例代码运行截图

在这里插入图片描述

3.2 方法的重载

3.2.1 Math工具类里面一些方法,以abs为例(绝对值)
3.2.2 示例代码
Math类
 public static int abs(int a) {
        return (a < 0) ? -a : a;
    }

    /**
     * Returns the absolute value of a {@code long} value.
     * If the argument is not negative, the argument is returned.
     * If the argument is negative, the negation of the argument is returned.
     *
     * <p>Note that if the argument is equal to the value of
     * {@link Long#MIN_VALUE}, the most negative representable
     * {@code long} value, the result is that same value, which
     * is negative.
     *
     * @param   a   the argument whose absolute value is to be determined
     * @return  the absolute value of the argument.
     */
    public static long abs(long a) {
        return (a < 0) ? -a : a;
    }

    /**
     * Returns the absolute value of a {@code float} value.
     * If the argument is not negative, the argument is returned.
     * If the argument is negative, the negation of the argument is returned.
     * Special cases:
     * <ul><li>If the argument is positive zero or negative zero, the
     * result is positive zero.
     * <li>If the argument is infinite, the result is positive infinity.
     * <li>If the argument is NaN, the result is NaN.</ul>
     * In other words, the result is the same as the value of the expression:
     * <p>{@code Float.intBitsToFloat(0x7fffffff & Float.floatToIntBits(a))}
     *
     * @param   a   the argument whose absolute value is to be determined
     * @return  the absolute value of the argument.
     */
    public static float abs(float a) {
        return (a <= 0.0F) ? 0.0F - a : a;
    }

    /**
     * Returns the absolute value of a {@code double} value.
     * If the argument is not negative, the argument is returned.
     * If the argument is negative, the negation of the argument is returned.
     * Special cases:
     * <ul><li>If the argument is positive zero or negative zero, the result
     * is positive zero.
     * <li>If the argument is infinite, the result is positive infinity.
     * <li>If the argument is NaN, the result is NaN.</ul>
     * In other words, the result is the same as the value of the expression:
     * <p>{@code Double.longBitsToDouble((Double.doubleToLongBits(a)<<1)>>>1)}
     *
     * @param   a   the argument whose absolute value is to be determined
     * @return  the absolute value of the argument.
     */
    public static double abs(double a) {
        return (a <= 0.0D) ? 0.0D - a : a;
    }
TestCat类
public class TestCat {
    public static void main(String[] args) {
        System.out.println(Math.abs(-10.9));
        System.out.println(Math.abs(-19));
    }
}

3.2.3 示例代码运行截图

在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

SSS4362

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值