package com.xgp.company.结构性模式.桥接模式; /** * 品牌类 */ public interface brand { void info(); }
package com.xgp.company.结构性模式.桥接模式; /** * 苹果品牌 */ public class apple implements brand { @override public void info() { system.out.print("苹果"); } }
package com.xgp.company.结构性模式.桥接模式; public class laptop extends computer { public laptop(brand brand) { super(brand); } @override protected void info() { super.info(); system.out.println("笔记本"); } }
package com.xgp.company.结构性模式.桥接模式; /** * 抽象的电脑类型类 */ public abstract class computer { //组合:品牌,电脑自带品牌 protected brand brand; public computer(brand brand) { this.brand = brand; } protected void info() { //自带品牌 brand.info(); } }
package com.xgp.company.结构性模式.桥接模式; public class desktop extends computer { public desktop(brand brand) { super(brand); } @override protected void info() { super.info(); system.out.println("台式机"); } }
package com.xgp.company.结构性模式.桥接模式; public class laptop extends computer { public laptop(brand brand) { super(brand); } @override protected void info() { super.info(); system.out.println("笔记本"); } }
package com.xgp.company.结构性模式.桥接模式; public class test { public static void main(string[] args) { //苹果笔记本 computer computer = new laptop(new apple()); computer.info(); //联想台式机 computer computer12 = new desktop(new lenovo()); computer12.info(); } }
苹果笔记本 联想台式机
如对本文有疑问, 点击进行留言回复!!
网友评论