• 猿创征文|《Java》【速学】类与继承


    一、理解继承的作用并学会使用继承

    这边的Dog与下面的Penguin有什么问题呢?如何优化呢
        - name:String
        - health:int
        - love:int
        - strain:String
        ----------------
        + print():void 
        + getName():String
        + getHealth():int
        + getLove():int
        + getStrain():String
        + Dog()
    }

    package o915;

    public class Pernguin {
        - name:String
        - health:int
        - love:int
        - sex:String
        ----------------
        + print():void
        + getName():String
        + getHealth():int
        + getLove():int
        + getSex():String
        +Pernguin()
    }

     由此得出:

    1. /* -------------------------------------------------------------------------------
    2. * [US-EN]:
    3. * (Attribution):
    4. * By SHaoYuDeng.( Tao Mo Si ).Full ownership and all rights reserved.
    5. * (File. Role):
    6. * System name: ka
    7. * 1.The source file [Pernguin.java] belongs to the underlying file.
    8. * 2.You can only limit the scope of this project [JavaSrc].
    9. * (The license):
    10. * 1.This source document is not commercial, but it is not non-commercial,
    11. * such as commercial need through the original author authorization,
    12. * If no authorization is available.
    13. * stop using it immediately and view related files.
    14. * 2.User ONLY HAS THE RIGHT TO SET WITHOUT THE RIGHT TO modify!!
    15. * (Contact):
    16. * [Email]: 2412874626@qq.com
    17. * [Phone]: + 86 17681306381
    18. * Copyright (c) 2022. 9
    19. * By SHaoYuDeng.( Tao Mo Si ).Full ownership and all rights reserved.
    20. * @version: 1.0
    21. * -------------------------------------------------------------------------------
    22. *
    23. */
    24. package o915;
    25. public class Penguin {
    26. private String name;
    27. private int height;
    28. private int wid;
    29. private String sex;
    30. public Penguin() {
    31. }
    32. public String getName() {
    33. return name;
    34. }
    35. public Penguin setName(String name) {
    36. this.name = name;
    37. return this;
    38. }
    39. public int getHeight() {
    40. return height;
    41. }
    42. public Penguin setHeight(int height) {
    43. this.height = height;
    44. return this;
    45. }
    46. public int getWid() {
    47. return wid;
    48. }
    49. public Penguin setWid(int wid) {
    50. this.wid = wid;
    51. return this;
    52. }
    53. public String getSex() {
    54. return sex;
    55. }
    56. public Penguin setSex(String sex) {
    57. this.sex = sex;
    58. return this;
    59. }
    60. public void print(){
    61. System.out.println("信息输出:"+this.getName()+" "+this.getHeight()+" "+this.getWid());
    62. }
    63. public Penguin(String name, int height, int wid, String sex) {
    64. super();
    65. this.name = name;
    66. this.height = height;
    67. this.wid = wid;
    68. this.sex = sex;
    69. }
    70. }
    71. package o915;
    72. public class Dog {
    73. private String name;
    74. private int height;
    75. public int wid;
    76. // strain
    77. private String strain;
    78. public Dog(){
    79. }
    80. public String getName() {
    81. return name;
    82. }
    83. public void setName(String name) {
    84. this.name = name;
    85. }
    86. public int getHeight() {
    87. return height;
    88. }
    89. public void setHeight(int height) {
    90. this.height = height;
    91. }
    92. public int getWid() {
    93. return wid;
    94. }
    95. public void setWid(int wid) {
    96. this.wid = wid;
    97. }
    98. public String getStrain() {
    99. return strain;
    100. }
    101. public void setStrain(String strain) {
    102. this.strain = strain;
    103. }
    104. public void print(){
    105. System.out.println("信息输出:"+this.getName()+" "+this.getHeight()+" "+this.getWid());
    106. }
    107. public Dog(String name, int height, String strain){
    108. this.name = name;
    109. this.height= height;
    110. this.strain = strain;
    111. }
    112. }

     我们可以将重复的代码抽取道【父类】中并使用【继承】来优化设计

    可以将dog和penguin里的类提取道Ani里

    实列:

    Ain文件

    1. package o915.ol;
    2. public class Ani {
    3. // 将demo01包中Dog类和Penguin类中相同的代码提取到这个Animal类中
    4. private String name;
    5. private int health;
    6. private int love;
    7. public Ani() {
    8. }
    9. public Ani(String name, int health, int love) {
    10. this.name = name;
    11. this.health = health;
    12. this.love = love;
    13. }
    14. public String getName() {
    15. return name;
    16. }
    17. public void setName(String name) {
    18. this.name = name;
    19. }
    20. public int getHealth() {
    21. return health;
    22. }
    23. public void setHealth(int health) {
    24. this.health = health;
    25. }
    26. public int getLove() {
    27. return love;
    28. }
    29. public void setLove(int love) {
    30. this.love = love;
    31. }
    32. public void print() {
    33. System.out.println("宠物信息-->昵称:" + this.getName() + ",健康值:"
    34. + this.getHealth() + ",亲密度:" + this.getLove());
    35. }
    36. }
    Penguin文件
    
    1. package o915.ol;
    2. public class Penguin extends Ani {
    3. //定义企鹅类中特有的属性
    4. private String sex;
    5. public Penguin() {
    6. super();//表示使用Animal类中的无参构造方法
    7. }
    8. public Penguin(String name, int health, int love, String sex) {
    9. super(name, health, love);
    10. this.sex = sex;
    11. }
    12. public String getSex() {
    13. return sex;
    14. }
    15. public void setSex(String sex) {
    16. this.sex = sex;
    17. }
    18. //定义Penguin类中特有的方法
    19. public void swimming(){
    20. System.out.println("企鹅会仰泳.......");
    21. }
    22. }
    Dog文件
    1. package o915.ol;
    2. public class Dog extends Ani {
    3. //在这个Dog类中只定义Dog类中特有的属性和方法,原来和Penguin类中相同的代码在Animal类中,通过继承获取,使用extends关键字来获取
    4. private String strain;
    5. public Dog() {
    6. super();//表示使用Animal类中的无参构造方法
    7. }
    8. public Dog(String name, int health, int love, String strain) {
    9. super(name, health, love);//表示使用Animal类中的有参构造方法
    10. this.strain = strain;
    11. }
    12. public String getStrain() {
    13. return strain;
    14. }
    15. public void setStrain(String strain) {
    16. this.strain = strain;
    17. }
    18. //定义Dog类中特有的方法
    19. public void eat(){
    20. System.out.println("狗吃骨头......");
    21. }
    22. public void printInfo(){
    23. super.print();
    24. System.out.println("品种:"+this.getStrain());
    25. }
    26. }
    Test文件
    1. package o915.ol;
    2. public class Test {
    3. public static void main(String[] args) {
    4. //创建Dog类对象
    5. Dog dog1 = new Dog();
    6. dog1.setName("二哈");
    7. dog1.setHealth(100);
    8. dog1.setLove(100);
    9. dog1.setStrain("喵咪");
    10. //print()方法是Animal类中的
    11. dog1.print();
    12. //eat()方法是Dog类中的
    13. dog1.eat();
    14. dog1.printInfo();
    15. Penguin p1 = new Penguin("企鹅", 99, 88, "公");
    16. //print()方法是Animal类中的
    17. p1.print();
    18. //swimming()是Penguin类中的
    19. p1.swimming();
    20. }
    21. }

    信息台输出:

    1. 宠物信息-->昵称:二哈,健康值:100,亲密度:100
    2. 狗吃骨头......
    3. 宠物信息-->昵称:二哈,健康值:100,亲密度:100
    4. 品种:喵咪
    5. 宠物信息-->昵称:企鹅,健康值:99,亲密度:88
    6. 企鹅会仰泳.......

    可以看到继承关系的复杂与好处,设顶的子类可以继承设置好的父类,从而获得信息的汇总与分发

  • 相关阅读:
    【Leetcode】664. Strange Printer(配数学证明)
    规格模式 Specification Pattern
    geth共识替换方法
    如何从戴尔笔记本电脑硬盘恢复数据
    JVS无忧·企业计划2.1.6更新说明
    akamai相关资料整理
    期末前端web大作业——名侦探柯南网页制作 Hbuiderx制作网页 静态HTML网页单页制作 dreamweaver网页设计与制作代码 web前端期末大作业
    JVM调优篇:探索Java性能优化的必备种子面试题
    DBPack SQL Tracing 功能及数据加密功能详解
    判断js中变量的类型的方法
  • 原文地址:https://blog.csdn.net/qq_39395109/article/details/126879137