编辑代码

class Main {
	public static void main(String[] args) {
		System.out.println("Hello world!   - java.jsrun.net ");
        show(new cat());
        show(new dog());
        System.out.println("------------");
        
        animal a = new cat();
        a.eat();
        cat c = (cat)a;
        c.work();
	}
    public static void show(animal a)
    {
        a.eat();
        if(a instanceof cat)
        {
            cat c = (cat)a;
            c.work();
        }
        else if(a instanceof dog)
        {
            dog c = (dog)a;
            c.work();
        }
    }
}
abstract class animal
{
    abstract void eat();
}
class cat extends animal
{
    public void eat()
    {
        System.out.println("eating fish.");
    }
    public void work()
    {
        System.out.println("catching mouse.");
    }
}
class dog extends animal
{
    public void eat()
    {
        System.out.println("eating meat.");
    }
    public void work()
    {
        System.out.println("barking.");
    }
}