public class Hammer { public String function(){ return "用锤子修理东西"; } } public class Worker { public void fix(Hammer hammer){ System.out.println("工人" hammer.function()); } public static void main(String[] args) { new Worker().fix(new Hammer()); } }
public interface Tools { public String function(); }
public class Worker { public void fix(Tools tool){ System.out.println("工人" tool.function()); } public static void main(String[] args) { new Worker().fix(new Hammer()); new Worker().fix(new Screwdriver()); } }
public class Hammer implements Tools{ public String function(){ return "用锤子修理东西"; } } public class Screwdriver implements Tools{ @Override public String function() { return "用螺丝刀修理东西"; } }