Leemaasn(我给大家拜早年啦!新春快乐!!!)
你到处UP啊:??三版?看的是英文的??

解决方案 »

  1.   

    精简版本
    class FruitQualities {
    private int weight;
    private int color;
    private int firmness;
    private int ripeness;
    private int smell;
    /* Default constructor */
    public FruitQualities() {
    }
    /* Copy constructor */
    public FruitQualities(FruitQualities f) {
    weight = f.weight;
    color = f.color;
    firmness = f.firmness;
    ripeness = f.ripeness;
    smell = f.smell;
    }
    }
    class Seed {
    public Seed() { /* Default constructor */
    }
    public Seed(Seed s) { /* Copy constructor */
    }
    }
    class Fruit {
    private FruitQualities fq;
    private int seeds;
    private Seed[] s;
    public Fruit(FruitQualities q, int seedCount) {
    fq = q;
    seeds = seedCount;
    s = new Seed[seeds];
    for (int i = 0; i < seeds; i++)
    s[i] = new Seed();
    }
    // Copy constructor:
    public Fruit(Fruit f) {
    fq = new FruitQualities(f.fq);
    seeds = f.seeds;
    s = new Seed[seeds];
    // Call all Seed copy-constructors:
    for (int i = 0; i < seeds; i++)
    s[i] = new Seed(f.s[i]);
    }
    }
    class Tomato extends Fruit {
    public Tomato() {
    super(new FruitQualities(), 100);
    }
    public Tomato(Tomato t) { // Copy-constructor
    super(t); // Upcast for base copy-constructor
    // Other copy-construction activities...
    }
    }
    public class B { public static void ripen(Tomato t) {
    // Use the "copy constructor":
    t = new Tomato(t);
    System.out.println("In ripen, t is a " + t.getClass().getName());
    }
    public static void slice(Fruit f) {
    f = new Fruit(f); // Hmmm... will this work?
    System.out.println("In slice, f is a " + f.getClass().getName());
    }
    public static void main(String[] args) {
    Tomato tomato = new Tomato();
    ripen(tomato); // OK
    slice(tomato); // OOPS!
    }
    } ///:~
      

  2.   

    再精简
    class FruitQualities {
    private int weight;
    public FruitQualities() {}/* Default constructor */
    public FruitQualities(FruitQualities f) {
    weight = f.weight;/* Copy constructor */}
    }
    class Seed {
    public Seed() { /* Default constructor */}
    public Seed(Seed s) { /* Copy constructor */}
    }
    class Fruit {
    private FruitQualities fq;
    private int seeds;
    private Seed[] s;
    public Fruit(FruitQualities q, int seedCount) {
    fq = q;
    seeds = seedCount;
    s = new Seed[seeds];
    for (int i = 0; i < seeds; i++)
    s[i] = new Seed();}
    // Copy constructor:
    public Fruit(Fruit f) {
    fq = new FruitQualities(f.fq);
    seeds = f.seeds;
    s = new Seed[seeds];
    // Call all Seed copy-constructors:
    for (int i = 0; i < seeds; i++)
    s[i] = new Seed(f.s[i]);}
    }
    class Tomato extends Fruit {
    public Tomato() {
    super(new FruitQualities(), 100);}
    public Tomato(Tomato t) { // Copy-constructor
    super(t); // Upcast for base copy-constructor}
    }
    public class B {
    public static void ripen(Tomato t) {
    // Use the "copy constructor":
    t = new Tomato(t);
    System.out.println("In ripen, t is a " + t.getClass().getName());
    }
    public static void slice(Fruit f) {
    f = new Fruit(f); // Hmmm... will this work?
    System.out.println("In slice, f is a " + f.getClass().getName());
    }
    public static void main(String[] args) {
    Tomato tomato = new Tomato();
    ripen(tomato); // OK
    slice(tomato); // OOPS!}
    }