继承

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

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

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

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

}

 


posted @ 2017-04-21 14:57  秋日的雨  阅读(101)  评论(0编辑  收藏  举报