VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > 编程开发 > Java教程 >
  • java入门之《 Java 编程思想》CH08 多态(2)

// Brass.play() n = MIDDLE_C // Woodwind.play() n = MIDDLE_C // Instrument // Percussion // Stringed // Instrument // Woodwind

练习7

class Piano extends Instrument {
    @Override
    void play(Note n) {
        System.out.println("Piano.play() n = " + n);
    }

    @Override
    public String toString() {
        return "Piano";
    }

    @Override
    void adjust() {
        System.out.println("Adjusting Piano");
    }
}

public class Music3 {
    public static void tune(Instrument i) {
        i.play(Note.MIDDLE_C);
    }

    public static void tuneAll(Instrument[] instruments) {
        for (Instrument instrument: instruments) {
            tune(instrument);
        }
    }

    public static void main(String[] args) {
        Instrument[] instruments = {
                new Wind(),
                new Percussion(),
                new Stringed(),
                new Brass(),
                new Woodwind(),
                new Piano(),
        };
        tuneAll(instruments);
        for (Instrument instrument: instruments) {
            System.out.println(instrument);
        }
    }
}
// Wind.play() n = MIDDLE_C
// Percussion.play() n = MIDDLE_C
// Stringed.play() n = MIDDLE_C
// Brass.play() n = MIDDLE_C
// Woodwind.play() n = MIDDLE_C
// Piano.play() n = MIDDLE_C
// Instrument
// Percussion
// Stringed
// Instrument
// Woodwind
// Piano

练习8

class InstrumentGenerator {
    private Random random = new Random(42);
    public Instrument next() {
        switch (random.nextInt(6)) {
            default:
            case 0: return new Wind();
            case 1: return new Percussion();
            case 2: return new Stringed();
            case 3: return new Brass();
            case 4: return new Woodwind();
            case 5: return new Piano();
        }
    }
}

public class Music3 {
    public static void tune(Instrument i) {
        i.play(Note.MIDDLE_C);
    }

    public static void tuneAll(Instrument[] instruments) {
        for (Instrument instrument: instruments) {
            tune(instrument);
        }
    }



    public static void main(String[] args) {
        Instrument[] instruments = new Instrument[10];
        InstrumentGenerator instrumentGenerator = new InstrumentGenerator();
        for (int i = 0;i < 10; i++) {
            instruments[i] = instrumentGenerator.next();
        }
        tuneAll(instruments);
        for (Instrument instrument: instruments) {
            System.out.println(instrument);
        }
    }
}

// Stringed.play() n = MIDDLE_C
// Brass.play() n = MIDDLE_C
// Wind.play() n = MIDDLE_C
// Stringed.play() n = MIDDLE_C
// Wind.play() n = MIDDLE_C
// Percussion.play() n = MIDDLE_C
// Piano.play() n = MIDDLE_C
// Stringed.play() n = MIDDLE_C
// Percussion.play() n = MIDDLE_C
// Piano.play() n = MIDDLE_C
// Stringed
// Instrument
// Instrument
// Stringed
// Instrument
// Percussion
// Piano
// Stringed
// Percussion
// Piano

练习9

package com.company.ch08;

public class Rodent {
    void eat() {
        System.out.println("Rodent.eat()");
    }

    public static void main(String[] args) {
        Rodent[] rodents = new Rodent[] {
                new Rodent(),
                new Mouse(),
                new Gerbil(),
                new Hamster(),
        };
        for (Rodent rodent: rodents) {
            rodent.eat();
        }
    }
}

class Mouse extends Rodent {
    @Override
    void eat() {
        System.out.println("Mouse.eat()");
    }
}

class Gerbil extends Rodent {
    @Override
    void eat() {
        System.out.println("Gerbil.eat()");
    }
}

class Hamster extends Rodent {
    @Override
    void eat() {
        System.out.println("Hamster.eat()");
    }
}
// Rodent.eat()
// Mouse.eat()
// Gerbil.eat()
// Hamster.eat()

练习10

package com.company.ch08;

class Base {
    void func1() {
        func2();
    }
    void func2() {
        System.out.println("Base");
    }
}

public class Ex10 extends Base {
    @Override
    void func2() {
        System.out.println("Ex10");
    }

    public static void main(String[] args) {
        Base base = new Ex10();
        base.func1();
    }
}
// Ex10

因为func2既不是static也不是final,所以他是动态绑定的,因此基类的 func1 中调用 func2 方法也是调用到导出类的 func2。

练习11

package com.company.ch08;

class Meal {
    Meal() {
        System.out.println("Meal()");
    }
}

class Bread {
    Bread() {
        System.out.println("Bread()");
    }
}

class Cheese {
    Cheese() {
        System.out.println("Cheese()");
    }
}

class Lettuce {
    Lettuce() {
        System.out.println("Lettuce()");
    }
}

class Lunch extends Meal {
    Lunch() {
        System.out.println("Lunch()");
    }
}

class PortableLunch extends Lunch {
    PortableLunch() {
        System.out.println("PortableLunch()");
    }
}

class Pickle {
    Pickle() {
        System.out.println("Pickle");
    }
}

public class Sandwich extends PortableLunch {
    private Bread b = new Bread();
    private Cheese c = new Cheese();
    private Lettuce l = new Lettuce();
    private Pickle p = new Pickle();
    public Sandwich() {
        System.out.println("Sandwich()");
    }

    public static void main(String[] args) {
        new Sandwich();
    }
}

练习12

package com.company.ch08;

public class Rodent {
    Rodent() {
        System.out.println("Rodent");
    }
    void eat() {
        System.out.println("Rodent.eat()");
    }

    public static void main(String[] args) {
        Rodent[] rodents = new Rodent[] {
                new Rodent(),
                new Mouse(),
                new Gerbil(),
                new Hamster(),
        };
        for (Rodent rodent: rodents) {
            rodent.eat();
        }
    }
}

class Mouse extends Rodent {
    Mouse() {
        System.out.println("Mouse");
    }
    @Override
    void eat() {
        System.out.println("Mouse.eat()");
    }
}

class Gerbil extends Rodent {
    Gerbil() {
        System.out.println("Gerbil");
    }
    @Override
    void eat() {
        System.out.println("Gerbil.eat()");
    }
}

class Hamster extends Rodent {
    Hamster() {
        System.out.println("Hamster");
    }
    @Override
    void eat() {
        System.out.println("Hamster.eat()");
    }
}

练习13

package com.company.ch08;

class Shared {
    private int refcount = 0;
    private static long counter = 0;
    private final long id = counter++;
    public Shared() {
        System.out.println("Create " + this);
    }
    void addRef() {
        refcount++;
    }
    protected void dispose() {
        if(--refcount == 0)
            System.out.println("Disposing " + this);
    }

    @Override
    public String toString() {
        return "Shared{" +
                "id=" + id +
                '}';
    }

    @Override
    protected void finalize() throws Throwable {
        System.out.println("finalize()");
        if (refcount != 0) {
            System.out.println("refcount != 0");
        }
        super.finalize();
    }
}

class Composing {
    private Shared shared;
    private static long counter = 0;
    private final long id = counter++;
    public Composing(Shared shared) {
        System.out.println("Creating " + this);
        this.shared = shared;
        this.shared.addRef();
    }
    protected void dispose() {
        System.out.println("disposing " + this);
        shared.dispose();
    }

    @Override
    public String toString() {
        return "Composing{" +
                "id=" + id +
                '}';
    }
}

public class ReferenceCounting {
    public static void main(String[] args) {

//        Shared shared = new Shared();
//        Composing[] composings = {
//                new Composing(shared), new Composing(shared),
//                new Composing(shared), new Composing(shared),
//                new Composing(shared)
//        };
//
//        for (Composing composing: composings) {
//            composing.dispose();
//        }
        new Composing(new Shared());
        System.gc();
    }
}

练习14

练习15

package com.company.ch08;

class Glyph {
    void draw() {
        System.out.println("Glyph.draw()");
    }
    Glyph() {
        System.out.println("Glyph() before draw()");
        draw();
        System.out.println("Glyph() after draw()");
    }
}

class RoundGlyph extends Glyph {
    private int radius = 1;
    RoundGlyph(int r) {
        radius = r;
        System.out.println("RoundGlyph.RoundGlyph(), radius = " + radius);
    }

    @Override
    void draw() {
        System.out.println("RoundGlyph.draw(), radius = " + radius);
    }
}

class RectangularGlygh extends Glyph {
    private int length;
    RectangularGlygh(int length) {
        this.length = length;
        System.out.println("RectanguarGlygh length = " + length);
    }

    @Override
    void draw() {
        System.out.println("RectanguarGlygh.draw() length = " + length);
    }
}

public class PolyConstructors {
    public static void main(String[] args) {
        new RectangularGlygh(10);
    }
}
// Glyph() before draw()
// RectanguarGlygh.draw() length = 0
// Glyph() after draw()
// RectanguarGlygh length = 10

练习16

package com.company.ch08;

class Status {
    void func() {}
}

class StatusA extends Status {
    void func() {
        System.out.println("Status A");
    }
}

class StatusB extends Status {
    void func() {
        System.out.println("Status B");
    }
}

class StatusC extends Status {
    void func() {
        System.out.println("Status C");
    }
}

class AlterStatus {
    Status status = new StatusA();
    void A() {
        status = new StatusA();
    }
    void B() {
        status = new StatusB();
    }
    void C() {
        status = new StatusC();
    }
    void call() {
        status.func();
    }
}

public class Starship {
    public static void main(String[] args) {
        AlterStatus alterStatus = new AlterStatus();
        alterStatus.call();
        alterStatus.B();
        alterStatus.call();
        alterStatus.C();
        alterStatus.call();
        alterStatus.A();
        alterStatus.call();
    }
}
// Status A
// Status B
// Status C
// Status A

练习17

package com.company.ch08;

public class Cycle {
    void run() {
        System.out.println("Cycle run");
    }
    int wheels() {
        return 0;
    }
}

class Unicycle extends Cycle {
    @Override
    void run() {
        System.out.println("Unicycle run");
    }

    @Override
    int wheels() {
        return 1;
    }
    
    void balance() {}
}

class Bicycle extends Cycle {
    @Override
    void run() {
        System.out.println("Bicycle run");
    }

    @Override
    int wheels() {
        return 2;
    }

    void balance() {}
}

class Tricycle extends Cycle {
    @Override
    void run() {
        System.out.println("Tricycle run");
    }

    @Override
    int wheels() {
        return 3;
    }
}

class Test {
    static void ride(Cycle c) {
        c.run();
    }
    public static void main(String[] args) {
        


        Cycle[] cycles = new Cycle[]{new Unicycle(), new Bicycle(), new Tricycle()};
//        for(Cycle cycle: cycles) {
//            cycle.balance(); // 无法调用
//        }
        Unicycle unicycle = (Unicycle)cycles[0];
        Bicycle bicycle = (Bicycle)cycles[1];
        Tricycle tricycle = (Tricycle)cycles[2];
        
        unicycle.balance();
        bicycle.balance();
//        tricycle.balance(); //无法调用
    }
}
相关教程