解析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));
}
}