例4.17
JPetTest.java

interface JPet  
{
    void petSkill();

    void petVoice();
}

class JCat implements JPet  
{
    public void petSkill() // 实现JPet的petSkill()方法
    {
        System.out.println("I can climb trees");
    }

    public void petVoice() // 实现JPet的petVoice()方法
    {
        System.out.println("喵");
    }
}

class JDog implements JPet  
{
    public void petSkill() // 实现JPet的petSkill()方法
    {
        System.out.println("I can guard houses");
    }

    public void petVoice() // 实现JPet的petVoice()方法
    {
        System.out.println("汪");
    }
}

public class JPetTest  
{
    public static void main(String args[])
    {
        JCat cat = new JCat();
        cat.petSkill();
        cat.petVoice();
        JDog dog = new JDog();
        dog.petSkill();
        dog.petVoice();
    }
}

例4.18
JPetTest2.java

interface JPet2  
{
    String petName="宠物类";

    void petSkill();
}

class JCat2 implements JPet2  
{
    String  getpetName()
    {
        return petName+":猫";  //直接调用接口中的常量
    }

    public void petSkill() // 实现JPet的petSkill()方法
    {
        System.out.println("I can climb trees");
    }
}


public class JPetTest2  
{
    public static void main(String args[])
    {
        JCat2 cat2 = new JCat2();
        cat2.petSkill();
        System.out.println(cat2.getpetName());

    }
}

例4.19
JPetTest3.java

interface JAnimal  
{
    String getLegNumbers();
}

interface JPet3 extends JAnimal  
{
    String petName = "宠物类";

    void petSkill();
}

class JCat3 implements JPet3  
{
    String getpetName()
    {
        return petName + ":猫"; // 直接调用接口中的常量
    }

    public void petSkill() // 实现JPet的petSkill()方法
    {
        System.out.println("I can climb trees");
    }

    public String getLegNumbers()  //必须要实现JAnimal接口的方法,否则编译出错
    {
        return "猫有四条腿";
    }
}

public class JPetTest3  
{
    public static void main(String args[])
    {
        JCat3 cat3 = new JCat3();
        cat3.petSkill();
        System.out.println(cat3.getpetName());
        System.out.println(cat3.getLegNumbers());
    }
}

例4.20
JPetTest4.java

interface JPet4  
{
    void petSkill();

    void petVoice();
}

class JCat4 implements JPet4  
{
    public void petSkill() // 实现JPet的petSkill()方法
    {
        System.out.println("I can climb trees");
    }

    public void petVoice() // 实现JPet的petVoice()方法
    {
        System.out.println("喵");
    }
}

class JDog4 implements JPet4  
{
    public void petSkill() // 实现JPet的petSkill()方法
    {
        System.out.println("I can guard houses");
    }

    public void petVoice() // 实现JPet的petVoice()方法
    {
        System.out.println("汪");
    }

    public void feature()
    {
        System.out.println("Human's friend!");
    }
}

public class JPetTest4  
{
    public static void main(String args[])
    {
        JPet4 newCat = new JCat4();
        JPet4 newDog = new JDog4();
        newCat.petSkill();
        newCat.petVoice();
        newDog.petSkill();
        newDog.petVoice();
        //newDog.feature();    //只能使用接口中的方法
    }
}

例4.21
JPetTest5.java

interface JPet5  
{
    void petSkill();

    void name();
}

interface JRatHunter  
{
    void ratHunt();
}

interface JFishHunter  
{
    void fishHunt();
}

class SmallAnimal  
{
    public void name()
    {
        System.out.println("class: SmallAnimal---method: name");
    }
}

class JDog5 extends SmallAnimal implements JPet5  
{
    public void petSkill()
    {
        System.out.println("class: JDog5---method: petSkill");
    }

    public void name()
    {
        System.out.println("class: JDog5---method: name");
    }
}

class JCat5 extends SmallAnimal implements JPet5, JRatHunter, JFishHunter  
{
    public void petSkill()
    {
        System.out.println("class: JCat5---method: petSkill");
    }

    public void ratHunt()
    {
        System.out.println("class: JCat5---method: ratHunt");
    }

    public void fishHunt()
    {
        System.out.println("class: JCat5---method: fishHunt");
    }

}

public class JPetTest5  
{
    static void showPetSkill(JPet5 p)
    {
        p.petSkill();
    }

    static void showName(SmallAnimal s)
    {
        s.name();
    }

    static void A(JPet5 p)
    {
        p.petSkill();
        p.name();
    }

    static void B(JRatHunter r)
    {
        r.ratHunt();
    }

    static void C(JFishHunter f)
    {
        f.fishHunt();
    }

    static void D(SmallAnimal s)
    {
        s.name();
    }

    public static void main(String args[])
    {
        System.out.println("现在开始以JPet5接口作为参数传值--------");
        JPet5  a1=new JDog5();
        JPet5  b1=new JCat5();
        showPetSkill(a1);
        showPetSkill(b1);
        System.out.println("现在开始以SmallAnimal类作为参数传值--------");
        SmallAnimal a2=new JDog5();
        SmallAnimal b2=new JCat5();
        showName(a2);
        showName(b2);
        System.out.println("现在开始对JCat5类产生的对象测试--------");
        JCat5 c=new JCat5();
        A(c);
        B(c);
        C(c);
        D(c);
    }
}