------解决方案-------------------- 简单写下吧 public interface BuildObject { public void Build(); } public class Horse implements BuildObject { private Color color; //马的特征属性 ... public void Build() { System.out.println("变成马"); } } public class Person implements BuildObject { private Color color; //人的特征属性 ... public void Build() { System.out.println("变成人"); }
} public class XiaoBaiLong extends Person { public void change(BuildObject obj) { obj.Build(); } public void Build() { System.out.println("在西游记中,小白龙是个人"); } } public class TangSeng { private XiaoBaiLong xiaobailong; public TangSeng() {} //唐僧需求 public void need() { //唐僧需要小白龙变成马 BuildObject obj = new Horse(); xiaobailong.change(obj); //唐僧需要小白龙变成人 BuildObject obj = new Person(); xiaobailong.change(obj); //唐僧突然又想小白龙变成妖怪呢,只需新建一个继承BuildObject的类,比如Dragon, BuildObject obj = new Dragon(); xiaobailong.change(obj); }