OOA&D课堂练习06.doc

上传人:壹****1 文档编号:547460691 上传时间:2023-12-03 格式:DOC 页数:6 大小:93KB
返回 下载 相关 举报
OOA&D课堂练习06.doc_第1页
第1页 / 共6页
OOA&D课堂练习06.doc_第2页
第2页 / 共6页
OOA&D课堂练习06.doc_第3页
第3页 / 共6页
OOA&D课堂练习06.doc_第4页
第4页 / 共6页
OOA&D课堂练习06.doc_第5页
第5页 / 共6页
点击查看更多>>
资源描述

《OOA&D课堂练习06.doc》由会员分享,可在线阅读,更多相关《OOA&D课堂练习06.doc(6页珍藏版)》请在金锄头文库上搜索。

1、OOA&D 第六周小课题目1、民用电源为交流220V,某电器需要直流5V电源,使用适配器模式实现电源转换。对象适配器模式和类适配器模式UML如下图。分别使用对象适配器模式和类适配器模式代码实现该结构。并构造客户端类。测试适配器功能。适配器 单抽象桥接 双抽象代码:/class V220Plugpackage week06_01;public class V220Plug public void GetPower() System.out.println(Show 220 valts ac。);/class V5Plugpackage week06_01;public class V5Plug

2、public void Get5VPower() System.out.println(Show 5 valts dc。);/class ObjectAdapterpackage week06_01;public class ObjectAdapter extends V220PlugV5Plug v5 = new V5Plug();public void GetPower() v5.Get5VPower();/class My220Plugpackage week06_01;public interface My220VPlug public void GetPower();/class C

3、lassAdapterpackage week06_01;public class ClassAdapter extends V5Plug implements My220VPlugpublic void GetPower() Get5VPower();/class Clientpackage week06_01;public class Client public static void main(String args) System.out.println(ObjectAdapter:); V220Plug electrical1 = new ObjectAdapter(); elect

4、rical1.GetPower(); System.out.println(ClassAdapter:); My220VPlug electrical2 = new ClassAdapter(); electrical2.GetPower(); 运行结果:2、在三种操作平台上Windows、Linux和Mactosh绘制四种图形Rectangle、Circle、Line和Square。即在不同平台上实现画图形Shape,而shape本身也变化。使用桥接模式可以隔离抽象类的变化和实现的变化。UML类图如下,用代码实现桥接模式的应用。并构造客户端类。实现在各种平台上画出各种图形。ShapeDraw

5、():voidRectangleCircleLineSquarePlatformOnPlatform():voidWindowsLinuxMactosh代码:/class Platformpackage week06_02;public abstract class Platform public abstract void OnPlatform();/class Windowspackage week06_02;public class Windows extends Platformpublic void OnPlatform() System.out.println(Using the

6、Windows System.);/class Linuxpackage week06_02;public class Linux extends Platformpublic void OnPlatform() System.out.println(Using the Linux System.);/class Mactoshpackage week06_02;public class Mactosh extends Platformpublic void OnPlatform() System.out.println(Using the Mactosh System.);/class Sh

7、apepackage week06_02;public abstract class Shape private Platform os; public Shape(Platform clientOS) os = clientOS; public void Draw() os.OnPlatform();/class Rectanglepackage week06_02;public class Rectangle extends Shapepublic Rectangle(Platform clientOS) super(clientOS);System.out.print(Write a r

8、ectangle.);/class Circlepackage week06_02;public class Circle extends Shapepublic Circle(Platform clientOS) super(clientOS);System.out.print(Write a Circle.);/class Linepackage week06_02;public class Line extends Shapepublic Line(Platform clientOS) super(clientOS);System.out.print(Write a line.);/cl

9、ass Squarepackage week06_02;public class Square extends Shapepublic Square(Platform clientOS) super(clientOS);System.out.print(Write a square.);/class Clientpackage week06_02;public class Client public static void main(String args) Rectangle r1=new Rectangle(new Windows(); r1.Draw(); Circle c1=new C

10、ircle(new Windows(); c1.Draw();Line l1=new Line(new Windows();l1.Draw();Square s1=new Square(new Windows();s1.Draw();System.out.println(=);Rectangle r2=new Rectangle(new Linux(); r2.Draw(); Circle c2=new Circle(new Linux(); c2.Draw();Line l2=new Line(new Linux();l2.Draw();Square s2=new Square(new Li

11、nux();s2.Draw();System.out.println(=);Rectangle r3=new Rectangle(new Mactosh(); r3.Draw(); Circle c3=new Circle(new Mactosh(); c3.Draw();Line l3=new Line(new Mactosh();l3.Draw();Square s3=new Square(new Mactosh();s3.Draw();System.out.println(=); 运行结果:要求:1. 使用 Java 、C# 或者 C+或其他OOPL如ruby、python等编程语言编写上边题中UML类图的代码,能运行通过实现程序的功能。

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 生活休闲 > 科普知识

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号