• 学习java的第十九天。。。(方法重写、Object类)


    方法重写

    子类根据需求对从父类继承的方法进行重新编写
    重写时,可以用super.方法的方式来保留父类的方法
    构造方法不能被重写

    就像之前写的代码:

    1. public class Pet {
    2. private String name;
    3. private int health;
    4. private int love;
    5. // 添加构造方法
    6. public Pet() {
    7. }
    8. public Pet(String name, int health, int love) {
    9. this.name = name;
    10. this.health = health;
    11. this.love = love;
    12. }
    13. public String getName() {
    14. return name;
    15. }
    16. public void setName(String name) {
    17. this.name = name;
    18. }
    19. public int getHealth() {
    20. return health;
    21. }
    22. public void setHealth(int health) {
    23. this.health = health;
    24. }
    25. public int getLove() {
    26. return love;
    27. }
    28. public void setLove(int love) {
    29. this.love = love;
    30. }
    31. public void print() {
    32. System.out.println(this.getName() + " " + this.getLove() + " "
    33. + this.getHealth());
    34. }
    35. }
    36. //Dog
    37. public class Dog extends Pet {
    38. //strain属性是Dog类里独有的属性,其他类没有,所以要在Dog类里定义
    39. private String strain;
    40. public Dog(){
    41. }
    42. public Dog(String name, int health, int love, String strain) {
    43. super(name, health, love);
    44. this.strain = strain;
    45. }
    46. public String getStrain() {
    47. return strain;
    48. }
    49. public void setStrain(String strain) {
    50. this.strain = strain;
    51. }
    52. }
    53. //测试类
    54. Dog dog1=new Dog("大黄", 66, 66, "柯基");
    55. //在dog类中没有setName方法,但是Dog类对象dog1是可以调用,因为Dog类继承了pet类
    56. dog1.setName("小黄");
    57. dog1.print();

     

    这里只能输出父类Pet的print()的方法, 并不能把Dog类中的strain属性输出,所以需要使用到方法重写

     为什么要使用方法重写

    父类的方法无法满足子类的需求,就需要使用到方法重写

    什么是方法重写

    子类根据需求对从父类继承的方法进行重新编写
    重写时,可以用super.方法的方式来保留父类的方法
    构造方法不能被重写

    1. //父类Pet
    2. public class Pet {
    3. private String name;
    4. private int health;
    5. private int love;
    6. // 添加构造方法
    7. public Pet() {
    8. //调用Pet类的父类Object类里的无参构造方法
    9. super();
    10. }
    11. public Pet(String name, int health, int love) {
    12. this.name = name;
    13. this.health = health;
    14. this.love = love;
    15. }
    16. public String getName() {
    17. return name;
    18. }
    19. public void setName(String name) {
    20. this.name = name;
    21. }
    22. public int getHealth() {
    23. return health;
    24. }
    25. public void setHealth(int health) {
    26. this.health = health;
    27. }
    28. public int getLove() {
    29. return love;
    30. }
    31. public void setLove(int love) {
    32. this.love = love;
    33. }
    34. public void print() {
    35. System.out.print("昵称:"+this.getName() + " 亲密度:" + this.getLove() + " 健康值"
    36. + this.getHealth());
    37. }
    38. }
    39. //Dog类
    40. public class Dog extends Pet {
    41. //strain属性是Dog类里独有的属性,其他类没有,所以要在Dog类里定义
    42. private String strain;
    43. public Dog(){
    44. }
    45. public Dog(String name, int health, int love, String strain) {
    46. super(name, health, love);
    47. this.strain = strain;
    48. }
    49. public String getStrain() {
    50. return strain;
    51. }
    52. public void setStrain(String strain) {
    53. this.strain = strain;
    54. }
    55. @Override
    56. public void print() {
    57. super.print();
    58. System.out.println(" 品种:"+this.getStrain());
    59. }
    60. }
    61. //penguin类
    62. public class Penguin extends Pet {
    63. //定义Penguin类里独有的属性
    64. private String sex;
    65. public Penguin(){
    66. super();//这个地方表示调用父类的无参构造方法
    67. }
    68. public Penguin(String name, int health, int love, String sex) {
    69. super(name, health, love);
    70. this.sex = sex;
    71. }
    72. public String getSex() {
    73. return sex;
    74. }
    75. public void setSex(String sex) {
    76. this.sex = sex;
    77. }
    78. public void print() {
    79. super.print();
    80. System.out.println(" 性别:"+this.getSex());
    81. }
    82. }
    83. //测试类
    84. Dog dog = new Dog("旺旺", 5, 8, "恶霸犬");
    85. dog.print();
    86. Penguin penguin = new Penguin("QQ", 0, 1, "雄");
    87. penguin.print();

     输出结果

    方法重写的规则

    需要是该类的子类
    方法名相同
    参数列表相同
    返回值类型相同或者是其子类
    访问权限不能严于父类

    方法重写与方法重载

    位置方法名参数表返回值访问权限修饰符
    方法重写子类相同相同相同或者是子类不能比父类更严格
    方法重载同类相同不同无关无关

    Object类

    Object类是所有类的父类

    Object被子类经常重写的方法

    toString()返回当前对象本身的有关信息
    equals()比较两个对象是否是同一个对象,是则返回true
    hashCode()返回改对象的哈希代码值
    getClass()获取当前对象所属的类信息,返回Class对象

    toString对象的重写

    若不重写toString,输出的则是:包名.类名.@.hash码值,是地址;若重写之后则输出对象本身的值。

    1. public static void main(String[] args) {
    2. //创建学生类对象
    3. Student stu = new Student("马力", 66);
    4. System.out.println(stu);//cn.bdqn.demo02.Student@40671416
    5. String str=stu.toString();//按住ctrl点击toString可以查看
    6. System.out.println(str);//cn.bdqn.demo02.Student@40671416
    7. System.out.println(stu.getClass().getName()+'@'+Integer.toHexString(stu.hashCode()));//cn.bdqn.demo02.Student@40671416
    8. }

     可以按住ctrl,点击toString

    1. @Override
    2. public String toString() {
    3. return "姓名:"+this.getName()+",年龄:"+this.getAge();
    4. }

     重写之后输出的代码是

     还可以右击鼠标,——>Source——>Generate toString

     

     这里可以选择在toString中重写哪些方法

    equals()方法

    为什么要重写equals()方法:

     object类中的equals()方法比较是的两个对象的地址值,不能满足子类的需求

       “==”表示比较两个基本数据类型,“equals”表示比较两个对象。若不引入object类的equals方法,则输出为对象的地址,而引入以后则输出对象的值。

    1. public static void main(String[] args) {
    2. Dog dog3 = new Dog("马力", 78, 56);
    3. System.out.println(dog3);
    4. System.out.println(dog3.toString());
    5. Dog dog2 = new Dog("马力", 78, 56);
    6. /*
    7. * object类中的equals()方法比较是的两个对象的地址值,不能满足子类的需求:
    8. * string类里只要两个字符串对象的内容相同,就认为两个对象是同一个对象,所以String类重写了0bject类里的equals()方法
    9. * Dog类里只要两个Dog对象的昵称、品种和健康值一致,就认为两个Dog对象是同一个对象,所以也需要在Dog类里重写Object里的equals()方法
    10. */
    11. boolean result = dog2.equals(dog3);
    12. System.out.println(result);//false
    13. System.out.println(dog3);
    14. System.out.println(dog2);
    15. String str1 = new String("dsad");
    16. String str2 = new String("dsad");
    17. System.out.println(str1.equals(str2));
    18. }

  • 相关阅读:
    C++【string类】
    从零开始打造个性化生鲜微信商城小程序
    【王道】数据结构与算法之排序算法(十一)
    深度学习基础网络整理----AlexNet
    android 如何分析应用的内存(十五)——Visual Studio Code 调试Android应用
    【华为云云耀云服务器L实例评测|使用教学】一文带你快速入手华为云云耀云服务器L实例
    Linux网络设置
    比色免疫测定:Enzo CHO宿主细胞蛋白ELISA试剂盒方案
    java-数组
    智慧新能源电站远程监控系统总体设计
  • 原文地址:https://blog.csdn.net/qihaojinqiuma/article/details/126158737