日期:2014-05-20 浏览次数:20788 次
/* filename: InnerClassUpperCase2.java */ package cs.ch7; class Cat { public void eat(Fish fish) { System.out.println("猫正在吃"+fish); } } interface Fish { } class PetStore { private class Sardine implements Fish { private String name; public Sardine() { name="沙丁鱼"; } public String toString() { return name; } } private class Herring implements Fish { private String name; public Herring() { name="青鱼"; } public String toString() { return name; } } private class Tuna implements Fish { private String name; public Tuna() { name="金枪鱼"; } public String toString() { return name; } } public Fish provideSardine() { return new Sardine(); } public Fish provideHerring() { return new Herring(); } public Fish provideTuna() { return new Tuna(); } } //====================================== class InnerClassUpperCase2 测试类,含main() //==== public class InnerClassUpperCase2 { public static void main(String[] args) { Cat cat=new Cat(); //生成Cat类的对象。 PetStore petStore=new PetStore(); //生成Petstore对象。 cat.eat(petStore.provideSardine()); cat.eat(petStore.provideHerring()); cat.eat(petStore.provideTuna()); } } //====================================== end class InnerClassUpperCase2