解析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;}@Overridepublic 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 示例代码运行截图

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

  1. Java中方法的重载(overload)与重写/覆写(override)

    重载-Overload 函数的方法参数个数或类型不一致,称为方法的重载. 从含义上说,只要求参数的个数或参数的类型不一致就说两个函数是重载函数,而至于返回值是否一样,没关系.同时,重载可以发生在同一个 ...

  2. 【JavaSE05】Java中方法与重载、递归

    1.方法的声明和调用 什么是方法?为什么需要方法?代码复用,方便软件升级 什么是方法? 具备特定功能的一段独立的代码段 标准的方法格式:(注意格式的顺序) 修饰符 返回值类型 方法名(参数类型 参数名 ...

  3. 【JavaSE05】Java中方法与重载、递归-练习

    1.使用的递归的方法求5! public class DiGui{public static void main(String[] args){//使用的递归的方法求5!System.out.prin ...

  4. 【JavaSE05】Java中方法与重载、递归-思维导图

    思维导图看不清楚时: 1)可以将图片另存为图片,保存在本地来查看 2)右击在新标签中打开放大查看

  5. Java中方法的重载详解

    博主前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住也分享一下给大家,

  6. Java中 List、Set、Map 之间的区别

      小博此篇记录了开发过程中常用的几种集合详解,三者的区别对比均从IDEA相关层次图里面所得知,基于JDK8,如有错误欢迎批评指正. List(列表)   List的元素以线性方式存储,可以存放重复对 ...

  7. 面试题:Java中list、set、map之间的区别

    1.集合的体系 为什么要了解list.set.map呢? 答:我们在编写程序的过程中经常会对容器中的元素进行增删改查,那么如何快速又准 确的定位到你想访问到的元素呢?就不得不提到我们的常用的单列结合C ...

  8. java中函数的重载_Java中函数的重载

    函数的重载 1.同一个类 2.同名函数 3.参数个数不同或者参数类型不同 4.java是严谨性语言,如果函数出现的调用的不确定性,会编译失败. public static int add(int a, ...

  9. 大数据笔记2019.5.9 Java中方法的使用

    方法: 1.方法的概念:(函数/过程) 封装了一段特定的业务逻辑功能 尽可能的独立,一个方法只让干一件事: 方法可以被反复的重新的调用 减少代码的重复,有利于代码的维护,减少团队开发的成本提高开发的效 ...

最新文章

  1. MVC 中的 ViewModel
  2. 对于初学者Python开发难学吗?适合初学者吗?
  3. jquery带token访问接口ajax
  4. asp.net过滤HTML标签的几个函数
  5. python中any的妙用
  6. IBM也要开源机器学习平台
  7. java 单链表约瑟夫环_java循环单链表实现约瑟夫环问题
  8. matlab第二次上机作业答案,第二次上机作业
  9. 按值传递时 php必须复制值,PHP笔试题汇总
  10. tensorboard的可视化及模型可视化
  11. 计算机管理员无法创建密码,找到电脑管理员的密码
  12. python输入字符串str_python字符串String模块
  13. php留言系统源码,XYCMS php留言板 v8.0
  14. 橱柜衣柜 sketchup草图大师设计全屋定制家具意义?谈单拆单生产一起做了?-有屋软件
  15. pentaho资源库迁移-MySQL
  16. 金笛全新技术架构,鲲鹏击浪从兹始
  17. 二十六万字详解bat文件
  18. 服务器被攻击怎么办?如何防止服务器被攻击?
  19. 【机器学习】机器学习的基本概念/术语2
  20. POJ3159 Candies(差分约束)

热门文章

  1. linux时间管理,时钟中断,系统节拍
  2. Md5加密原理及其实现算法
  3. 聆听东方的罗密欧与朱丽叶
  4. PyQt的一个UI单元测试框架思路
  5. 怎么把图片变漫画效果呢?这个小妙招轻松完成
  6. IDEA字体颜色、主题风格个性化 —— 手把手带你尽展个性
  7. miniImageNet数据集介绍
  8. NBA 篮球英语第二集
  9. 日语二级语法汇总(part15/16)
  10. 谷歌关键词排名大量消失原因【2023分析指南】