zoukankan      html  css  js  c++  java
  • 继承

    继承;从字面的意思上来看就是儿子继承爸爸的财产,爸爸有的属性和方法儿子可以继承。

    继承以后还可以重现修改,就是我们所说的重载。

    重载要注意几点,重载是子类继承父类,在父类的基础上坐修改。

    1重写方法和被重写的方法必须具有相同的方法名。

    2 重写方法和被重写的方法必须具有相同参数列表。

    3 重写方法和被重写的方法必须具有相同的返回值类型或是其子类。

    4重写的方法不能缩小被重写的而方法的访问权限。

    继承后优化的类图,下从代码的层面看一下
    package Pet;
    
    /**
     * 
     * @author apple宠物类狗狗的父类
     *
     */
    public class Pet {
        private String name = "无名氏";// 昵称
    
        private int health;// 健康值
        private int love;// 亲密度
    
        /**
         * 无参构造方法
         */
        public Pet() {
            this.health = 95;
            System.out.println("健康值"+health);
    
        }
    
        /**
         * 有参构造
         */
        
    
        public Pet(String names, int love ,int health){
            this.name = names;
            this.love = love;
            this.health=health;
    
        }
    
        public String getName() {
            return name;
        }
    
        public int getHealth() {
            return health;
        }
    
        public int getLove() {
            return love;
        }
    
        /**
         * 输入信息
         */
        public void print() {
            System.out.println("我的自白" + "健康值是" + this.health + ",狗狗昵称" + this.name + ",主人呢的喜爱程度" + love);
        }
    
    }
    package Pet;
    
    /**
     * 狗狗类宠物类的子类
     */
    
    public class Dog extends Pet {
        private String strain;// 品种
    
        public Dog(String names, String strain, int love, int health) {
            super(names,love,health);    
            this.strain=strain;
            //super.print();
        }
        public String getStrain(){
            return strain;
        }
        /**
         * 重写父类的print()方法
         */
        public void print(){
            super.print();
            System.out.println("我是一只"+this.strain);
        }
    
    }
    
    
    
    
    
  • 相关阅读:
    学习记录(1):intellij idea 导入gradle
    javac不是内部或外部命令
    robot framework测试https接口实例
    py文件变成可执行exe ,遇到的问题及解决方法
    python 实现爬虫下载网页的方法
    [Usaco2009 Open]干草堆
    【题解】[USACO17JAN]Balanced Photo G
    【题解】[BalticOI 2014]friends
    从恒定状态出发,求解未知状态
    Scoi 组队
  • 原文地址:https://www.cnblogs.com/liuxingzhi/p/6743640.html
Copyright © 2011-2022 走看看