日期:2014-05-20 浏览次数:20677 次
//按钮接口 public interface Button { public void color(); public void style(); } //默认button public class DefaultButton implements Button { private String color = "default"; private String style = "default sytle"; protected String getColor() { return color; } protected void setColor(String color) { this.color = color; } protected String getStyle() { return style; } protected void setStyle(String style) { this.style = style; } @Override public void color() { System.out.println("button color is " + getColor()); } @Override public void style() { System.out.println("button style is " + getStyle()); } } //红色按钮 public class RedButton implements Button{ private String color = "red"; private String style = "red sytle"; private DefaultButton db; public RedButton(DefaultButton db){ this.db = db; db.setColor(color); db.setStyle(style); } @Override public void color() { db.color(); } @Override public void style() { db.style(); } protected String getColor() { return color; } protected void setColor(String color) { this.color = color; } protected String getStyle() { return style; } protected void setStyle(String style) { this.style = style; } } //测试类 public class Test{ public static void main(String[] args) { RedButton rb = new RedButton(new DefaultButton()); rb.color(); rb.style(); } }