1..第一题
package net.mindview.polymorphism; //基类-自行车 class Cycle{ } //子类-单轮车 class Unicycle extends Cycle{ } //子类-双轮车 class Bicycle extends Cycle{ } //子类-三轮车 class Tricycle extends Cycle{ } public class CycleCompetition { public static void ride(Cycle cycle){ } public static void main(String[] args) { Bicycle b = new Bicycle(); ride(b); } } 你
2.(略)
3.(略)
4.第四题
package net.mindview.polymorphism; import java.util.Random; //形状 class Shape{ public void draw(){} public void erase(){} } //圆形 class Cycle1 extends Shape { @Override public void draw() { System.out.println("draw cycle"); } @Override public void erase() { System.out.println("erase cycle"); } } class Square extends Shape { @Override public void draw() { System.out.println("draw Square"); } @Override public void erase() { System.out.println("draw Square"); } } //矩形 class Triangle extends Shape { @Override public void draw() { System.out.println("draw Triangle"); } @Override public void erase() { System.out.println("draw Triangle"); } } //梯形 class Trapezoid extends Shape { @Override public void draw() { System.out.println("draw Trapezoid"); } @Override public void erase() { System.out.println("draw Trapezoid"); } } class RandomShapeFactory { Random random = new Random(100); //下面画什么形状呢 public Shape next(){ switch(random.nextInt(4)) { default: case 0: return new Cycle1(); case 1: return new Square(); case 2: return new Triangle(); case 3: return new Trapezoid(); } } } public class Shapes { public static void main(String[] args) { Shape[] shape = new Shape[9]; RandomShapeFactory factory = new RandomShapeFactory(); for(int i=0; i<shape.length; i++){ shape[i] = factory.next(); shape[i].draw(); } } }
5.第五题
package net.mindview.polymorphism; //基类-自行车 class Cycle{ public int wheels(){ return 0; } } //子类-单轮车 class Unicycle extends Cycle{ public int wheels(){ return 1; } } //子类-双轮车 class Bicycle extends Cycle{ public int wheels(){ return 2; } } //子类-三轮车 class Tricycle extends Cycle{ public int wheels(){ return 3; } } public class CycleCompetition { public static void ride(Cycle cycle){ System.out.println(cycle.wheels()); } public static void main(String[] args) { Bicycle b = new Bicycle(); ride(b); } }
6.第六题
package net.mindview.polymorphism.Music3; import net.mindview.polymorphism.Note; class Instrument { public void play(Note i){ System.out.println("Instrument.play() "); } public String toString () { return Instrument.class.getName(); } void adjust(){System.out.println("adjust Instrument");} } class Wind extends Instrument { @Override public void play(Note i) { System.out.println("Wind.play() "); } String what (){ return "Wind"; } void adjust(){System.out.println("adjust Wind");} } class Stringed extends Instrument { @Override public void play(Note i) { System.out.println("Stringed.play() "); } String what (){ return "Stringed"; } void adjust(){System.out.println("adjust Stringed");} } class Brass extends Instrument { @Override public void play(Note i) { System.out.println("Brass.play() "); } String what (){ return "Brass"; } void adjust(){System.out.println("adjust Brass");} } public class Music3 { //曲调 public static void tune(Instrument i){ i.play(Note.MIDDLE_C); } public static void tuneAll(Instrument[] i){ for(Instrument ins: i){ tune(ins); } } public static void main(String[] args) { Instrument[] i = { new Wind(), new Stringed(), new Brass() }; tuneAll(i); Instrument t = new Instrument(); System.out.println(t); } }
7.第七题
package net.mindview.polymorphism.Music3; import net.mindview.polymorphism.Note; class Instrument { public void play(Note i){ System.out.println("Instrument.play() "); } public String toString () { return Instrument.class.getName(); } void adjust(){System.out.println("adjust Instrument");} } class Wind extends Instrument { @Override public void play(Note i) { System.out.println("Wind.play() "); } String what (){ return "Wind"; } void adjust(){System.out.println("adjust Wind");} } class Stringed extends Instrument { @Override public void play(Note i) { System.out.println("Stringed.play() "); } String what (){ return "Stringed"; } void adjust(){System.out.println("adjust Stringed");} } class Brass extends Instrument { @Override public void play(Note i) { System.out.println("Brass.play() "); } String what (){ return "Brass"; } void adjust(){System.out.println("adjust Brass");} } class Other extends Instrument { @Override public void play(Note i) { System.out.println("Other.play() "); } String what (){ return "Other"; } void adjust(){System.out.println("adjust Other");} } public class Music3 { //曲调 public static void tune(Instrument i){ i.play(Note.MIDDLE_C); } public static void tuneAll(Instrument[] i){ for(Instrument ins: i){ tune(ins); } } public static void main(String[] args) { Instrument[] i = { new Wind(), new Stringed(), new Brass(), new Other() }; tuneAll(i); Instrument t = new Instrument(); System.out.println(t); } }
8.第八题
package net.mindview.polymorphism.Music3; import java.util.Random; import net.mindview.polymorphism.Note; class Instrument { public void play(Note i){ System.out.println("Instrument.play() "); } public String toString () { return Instrument.class.getName(); } void adjust(){System.out.println("adjust Instrument");} } class Wind extends Instrument { @Override public void play(Note i) { System.out.println("Wind.play() "); } String what (){ return "Wind"; } void adjust(){System.out.println("adjust Wind");} } class Stringed extends Instrument { @Override public void play(Note i) { System.out.println("Stringed.play() "); } String what (){ return "Stringed"; } void adjust(){System.out.println("adjust Stringed");} } class Brass extends Instrument { @Override public void play(Note i) { System.out.println("Brass.play() "); } String what (){ return "Brass"; } void adjust(){System.out.println("adjust Brass");} } class Other extends Instrument { @Override public void play(Note i) { System.out.println("Other.play() "); } String what (){ return "Other"; } void adjust(){System.out.println("adjust Other");} } class RandomInstrumentFactory { Random rand = new Random(100); public Instrument next(){ switch(rand.nextInt(4)){ default: case 0: return new Wind(); case 1: return new Stringed(); case 2: return new Brass(); case 3: return new Other(); } } } public class Music3 { //曲调 public static void tune(Instrument i){ i.play(Note.MIDDLE_C); } public static void tuneAll(Instrument[] i){ for(Instrument ins: i){ tune(ins); } } public static void main(String[] args) { RandomInstrumentFactory factory = new RandomInstrumentFactory(); Instrument[] instruments = new Instrument[10]; for(int i=0; i<instruments.length; i++){ instruments[i] = factory.next(); } tuneAll(instruments); Instrument t = new Instrument(); System.out.println(t); } }
9.第九题
package net.mindview.polymorphism; //啮(nie四声)齿动物 class Rodent{ void say(){System.out.println("hi,我是 Rodent");} } //老鼠 class Mouse extends Rodent{ void say(){System.out.println("hi,我是 Mouse");} } //鼹鼠 class Gerbil extends Rodent{ void say(){System.out.println("hi,我是 Gerbil");} } //大颊鼠 class Hamster extends Rodent{ void say(){System.out.println("hi,我是 Hamster");} } //啮(nie四声)齿动物大家族 public class RodentHome { public static void instroduce(Rodent rodent){ rodent.say(); } public static void instroduceAll(Rodent[] rodents){ for(Rodent r: rodents){ instroduce(r); } } public static void main(String[] args) { Rodent[] rodents = { new Mouse(), new Gerbil(), new Hamster() }; instroduceAll(rodents); } }
10.第十题
package net.mindview.polymorphism; class Base{ public void method1(){ System.out.println("method1"); method2(); } public void method2(){ System.out.println("Base method2"); } } class Child extends Base{ @Override public void method2() { System.out.println("Child method2"); } } public class Test10 { public static void up(Base b){ b.method1(); } public static void main(String[] args) { Child c = new Child(); up(c); } }
11.afd
12.adf
13.第十三题
package net.mindview.polymorphism; import static net.mindview.util.Print.*; //这是一个共享类 class Shared { private int refCount = 0; private static long counter = 0; private final long id = counter++; public Shared(){ println("Creating "+this); } public void addRef(){ refCount ++; } protected void dispose(){ if(--refCount ==0){ println("Disposing " + this); } } @Override public String toString() { return "Shared " + id; } @Override protected void finalize() throws Throwable { if(refCount != 0){ println("Error, 引用计数不为0"); } super.finalize(); } } //组成类 class Composing{ private Shared shared; private static long counter = 0; private final long id = counter ++; public Composing(Shared shared){ println("Creating "+ this); this.shared = shared; shared.addRef(); } protected void dispose() { println("dispose " + this); shared.dispose(); } @Override public String toString() { return "Composing " + id; } } //引用计数 public class ReferenceCounting { public static void main(String[] args) { // TODO Auto-generated method stub Shared shared = new Shared(); Composing[] composing = { new Composing(shared), new Composing(shared), new Composing(shared), new Composing(shared), new Composing(shared), new Composing(shared) }; for(Composing c:composing){ c.dispose(); } } }
14.fa
15.dfa
16.第十六题
package net.mindview.polymorphism; class AlertStatus { @Override public String toString() { return "AlertStatus"; } } class StartStatus extends AlertStatus { @Override public String toString() { return "StartStatus"; } } class EndStatus extends AlertStatus { @Override public String toString() { return "EndStatus"; } } class PauseStatus extends AlertStatus { @Override public String toString() { return "PauseStatus"; } } class Music4 { AlertStatus as = new StartStatus(); public void pause(){ as = new PauseStatus(); } public void end(){ as = new EndStatus(); } public void currentStatus(){ System.out.println(as); } } public class Starship { public static void main(String[] args) { Music4 m = new Music4(); m.currentStatus(); m.pause(); m.currentStatus(); m.end(); m.currentStatus(); } }
17.第十七题
package net.mindview.polymorphism; //基类-自行车 class Cycle{ public int wheels(){ return 0; } } //子类-单轮车 class Unicycle extends Cycle{ public int wheels(){ return 1; } public void balance(){ System.out.println("Unicycle balance"); } } //子类-双轮车 class Bicycle extends Cycle{ public int wheels(){ return 2; } public void balance(){ System.out.println("Bicycle balance"); } } //子类-三轮车 class Tricycle extends Cycle{ public int wheels(){ return 3; } } public class CycleCompetition { public static void ride(Cycle cycle){ System.out.println(cycle.wheels()); } public static void main(String[] args) { Cycle[] cycle = { new Cycle(), new Unicycle(), new Bicycle(), new Tricycle() }; //((Cycle)cycle[0]).balance();//编译不通过 ((Unicycle)cycle[1]).balance(); ((Bicycle)cycle[2]).balance(); } }