animal.java

来自「现在在国外大学里最流行的java学习软件,同时还有大量的example,在名为p」· Java 代码 · 共 102 行

JAVA
102
字号
import java.util.List;/** * Animal is an abstract superclass for animals. * It provides features common to all animals, * such as the location and age. *  * @author David J. Barnes and Michael Kolling * @version 2006.03.30 */public abstract class Animal{    // The animal's age.    private int age;    // Whether the animal is alive or not.    private boolean alive;    // The animal's position    private Location location;    /**     * Create a new animal with age zero (a new born).     */    public Animal()    {        age = 0;        alive = true;    }        /**     * Make this animal act - that is: make it do whatever     * it wants/needs to do.     * @param currentField The field currently occupied.     * @param updatedField The field to transfer to.     * @param newAnimals A list to add newly born animals to.     */    abstract public void act(Field currentField,                              Field updatedField, List<Animal> newAnimals);        /**     * Check whether the animal is alive or not.     * @return True if the animal is still alive.     */    public boolean isAlive()    {        return alive;    }    /**     * Tell the animal that it's dead now :(     */    public void setDead()    {        alive = false;    }        /**     * Return the animal's age.     * @return The animal's age.     */    public int getAge()    {        return age;    }    /**     * Set the animal's age.     * @param age The animal's age.     */    public void setAge(int age)    {        this.age = age;    }        /**     * Return the animal's location.     * @return The animal's location.     */    public Location getLocation()    {        return location;    }    /**     * Set the animal's location.     * @param row The vertical coordinate of the location.     * @param col The horizontal coordinate of the location.     */    public void setLocation(int row, int col)    {        this.location = new Location(row, col);    }    /**     * Set the animal's location.     * @param location The animal's location.     */    public void setLocation(Location location)    {        this.location = location;    }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?