public class Test_diversifyTest1 { public static void main(String[] args){ Cycle c = new Cycle(); print(" 车轮数:"+c.ride(neum.Bicycle).wh); Unicycle c1 = new Unicycle(); print(" 车轮数:"+c1.ride(neum.Unicycle).wh); } } class Cycle{ int wh = 0; public Cycle(){ print("This is in The class Cycle!"); } public Cycle ride(Object o){ print("Class: "+ this); return this; } }
class Unicycle extends Cycle{ int wh = 4; public Unicycle(){ print("This is in The class Unicycle!"); } } class Bicycle extends Cycle{ int wh = 2; public Bicycle(){ print("This is in The class Bicycle!"); } } class Tricycle extends Cycle{ int wh = 3; public Tricycle(){ print("This is in The Tricycle!"); } }
结果:
This is in The class Cycle! Class: Diversify.Cycle@61de33 车轮数:0 This is in The class Cycle! This is in The class Unicycle! Class: Diversify.Unicycle@ca0b6 车轮数:0