ImageVerifierCode 换一换
格式:DOCX , 页数:16 ,大小:343.82KB ,
资源ID:6458643      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/6458643.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(JAVA面向对象.docx)为本站会员(b****6)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

JAVA面向对象.docx

1、JAVA面向对象Java OOP Day01Top 1. 移方块游戏定义Rect类,编写测试方法testNew 2. 移方块游戏构建窗体,设置大小和位置 3. 移方块游戏添加Rect重载的构造方法 1 移方块游戏定义Rect类,编写测试方法testNew1.1 问题如何用Java代码表达移方块游戏中的矩形个体,且矩形对象要包含矩形的位置和大小特征,如图1(途中绿色方块是要被移动的矩形)所示。图11.2 方案计算机的绘图坐标系: 以矩形区域的左上角为原点(O), 原点开始由左至右为X轴方向, 原点开始由上至下为Y轴. 与数学平面坐标系不同: Y轴正方向相反.在计算机绘图坐标系中可以用4个属性明确

2、定义一个矩形, 分别是: x, y, width, height.如图2说明, 绘图坐标系与矩形个体的关系:图2解决方案:创建Java类Rect, 包含x, y, width, height 属性, 为Rect增加修改位置的方法setLocation() 和 修改大小的方法setSize().创建测试类TestRect, 添加测试方法testNew() 用于测试对象的创建, 和属性的访问.1.3 实现在Eclipse中创建类Rect 表示矩形类型:1. public class Rect2. /TODO 添加类的成员3. 在Rect类中添加属性: x, y, width, height1. i

3、nt x;2. int y;3. int width;4. int height;在Rect类添加修改位置的方法:1. public void setLocation(int x1, int y1) 2. x = x1;3. y = y1;4. 在Rect类中添加修改大小的方法:1. public void setSize(int w, int h) 2. width = w;3. height = h;4. 在Rect类中增加toString() 方法用于输出对象的文本描述, 方便调试输出:1. public String toString() 2. return x= + x + , y=

4、 + y + , width= + width + , height=3. + height;4. 创建测试类TestRect, 包含主方法1. public class TestRect2. public static void main(String args)3. /TODO 添加测试代码4. 5. 在测试类testRect添加测试代方法: testNew() 用于测试对象的创建1. public static void testNew() 2. Rect r1 = new Rect();3. Rect r2 = new Rect();4. r1.x = 100;5. r1.y = 20

5、0;6. r1.width = 50;7. r1.height = 60;8. System.out.println(r1);9. r2.x = 300;10. r2.y = 400;11. r2.width = 60;12. r2.height = 80;13. System.out.println(r2);14. 在主方法中添加测试语句调用测试方法testNew()1. public static void main(String args)2. testNew();3. 执行得到测试结果:1. x=100, y=200, width= 50, height=602. x=300, y=4

6、00, width= 60, height=80完成代码参考:1. /* 矩形类型 Rect.java */2. public class Rect 3. int x;4. int y;5. int width;6. int height;7. 8. public void setLocation(int x1, int y1) 9. x = x1;10. y = y1;11. 12. 13. public void setSize(int w, int h) 14. width = w;15. height = h;16. 17. 18. public String toString() 1

7、9. return x= + x + , y= + y + , width= + width + , height=20. + height;21. 22. 23. /* 测试类 TestRect.java */24. public class TestRect 25. 26. public static void main(String args) 27. testNew();28. 29. 30. public static void testNew() 31. Rect r1 = new Rect();32. Rect r2 = new Rect();33. r1.x = 100;34.

8、 r1.y = 200;35. r1.width = 50;36. r1.height = 60;37. System.out.println(r1);38. r2.x = 300;39. r2.y = 400;40. r2.width = 60;41. r2.height = 80;42. System.out.println(r2);43. 44. 1.4 扩展使用 = 运算符不能判断对象是否相等:使用new运算创建的对象, 是在不同物理地址上分配的物理对象, 引用的值是对象首地址的值, 即便属性值相同的对象, 也是不同的物理对象, 使用=运算只能判断引用值是否相等, 只能判断是否是同一个

9、物理位置对象. 不能用于判断对象属性是否相等.两个引用变量可以引用同一个对象.引用变量可以赋值, 赋值以后两个引用变量的值相同, 是同一个物理对象首地址值.在TestRect中添加测试方法:1. public static void testEquals() 2. Rect r1 = new Rect();3. r1.x = 100;4. r1.y = 200;5. System.out.println(r1);6. 7. Rect r2 = new Rect();8. r2.x = 100;9. r2.y = 200;10. System.out.println(r2);11. 12. Sy

10、stem.out.println(r1 = r2);13. 14. Rect r3 = r1;15. r3.x = 300;16. r3.y = 400;17. System.out.println(r3);18. System.out.println(r1);19. 20. 修改main方法, 添加语句调用测试方法testEquals():1. public static void main(String args) 2. /testNew();3. testEquals();4. 执行得到测试结果:1. x=100, y=200, width= 0, height=02. x=100, y

11、=200, width= 0, height=03. false4. x=300, y=400, width= 0, height=05. x=300, y=400, width= 0, height=02 移方块游戏构建窗体,设置大小和位置2.1 问题在系统中显示一个窗口,并且控制窗口的显示位置,如 图3所示(显示两个不同位置的窗口)。图32.2 方案利用Java系统Swing API 创建并且显示窗口, 控制窗口的位置. 如图4 窗口的位置为Location 是坐标(x,y), 窗口还有位置属性size包含两个值width和height.图 4Swing API类JFrame代表系统的窗口

12、框, 在系统类库javax.swing包中需要使用import语句导入到代码中使用:1. import javax.swing.JFrame;JFrame是类, 代表窗口框类型, 创建为对象代表一个窗口实例:1. JFrame frame = new JFrame(); JFrame 提供了API方法用于控制窗口的位置和大小, 以及设置窗口的标题内容:1. frame.setSize(500, 600);2. frame.setTitle(TestFrame);3. frame.setLocation(100, 100);在桌面上显示一个窗口使用setVisible(true)方法, 如果参数

13、是false就是关闭一个窗口对象.1. frame.setVisible(true);使用如下代码可以实现, 在关闭窗口时候同时退出应用程序.1. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);2.3 实现创建窗口测试类TestJFrame, 包含main方法用于启动测试案例.1. public class TestJFrame 2. public static void main(String args) 3. /TODO 添加测试方法调用4. 5. 在类的声明之前添加import语句导入Swing API JFrame类:1. i

14、mport javax.swing.JFrame;在测试类TestJFrame中增加测试方法创建窗口对象设置属性并且显示:1. public static void testSizeAndLocation() 2. JFrame frame = new JFrame();3. frame.setSize(500, 600);4. frame.setTitle(TestFrame);5. frame.setLocation(100, 100);6. frame.setVisible(true);7. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CL

15、OSE);8. 在main方法中添加调用测试方法语句, 调用测定方法:1. public class TestJFrame 2. public static void main(String args) 3. testSizeAndLocation();4. 5. 执行测试类TestJFrame 得到测试结果, 测试结果如图5所示。图5完成代码参考:1. import javax.swing.JFrame;2. 3. public class TestJFrame 4. public static void main(String args) 5. testSizeAndLocation();

16、6. testSizeAndCenter();7. 8. 9. public static void testSizeAndLocation() 10. JFrame frame = new JFrame();11. frame.setSize(500, 600);12. frame.setTitle(TestFrame);13. frame.setLocation(100, 100);14. frame.setVisible(true);15. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);16. 17. 2.4 扩展根据屏幕和窗口

17、的大小计算并且设置窗口的位置, 实现窗口的居中显示, 计算原理如图6所示。图6利用Swing的工具类可以获得当前系统的屏幕显示大小属性. 返回值是Dimension类型对象screen, 包含两个屏幕大小属性width, height:1. Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();在测试类中增加居中测试方法 testSizeAndCenter()1. public static void testSizeAndCenter() 2. JFrame frame = new JFrame();3. frame.s

18、etSize(500, 600);4. frame.setTitle(TestFrame);5. Dimension screen =6. Toolkit.getDefaultToolkit().getScreenSize();7. frame.setLocation(screen.width - frame.getWidth() / 2,8. (screen.height - frame.getHeight() / 2);9. frame.setVisible(true);10. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);11.

19、 在main方法中调用居中测试方法, 观察测试结果:1. public static void main(String args) 2. testSizeAndLocation();3. testSizeAndCenter();4. 执行结果, 出现居中的窗口, 如图7:图73 移方块游戏添加Rect重载的构造方法3.1 问题创建矩形实现和初始化矩形对象属性的代码过于复杂庸长:1. Rect r1 = new Rect();2. r1.x = 100;3. r1.y = 200;4. r1.width = 200;5. r1.height = 200;6. 7. Rect r2 = new R

20、ect();8. r2.x = 100;9. r2.y = 200;10. r2.width = 200;11. r2.height = 200;要提供更加便捷的创建对象实例的方法.并且要提供多种便捷的创建方式方法.3.2 方案提供更加便捷的创建矩形类的构造方法, 简化矩形对象的创建过程, 利用重载的构造器方便构建方式.3.3 实现重构Rect类增构造器, 用于创建并初始化对象, 构造器的名字要与类名一致(包括大小写也是一致的), 构造器不能声明返回值, 构造器中的语句用于初始化对象的属性值:1. public Rect(int x, int y, int width, int height)

21、 2. this.x = x;3. this.y = y;4. this.width = width;5. this.height = height;6. 重构TestRect类 增加构造器测试方法, 调用构造器初始化并创建对象:1. public static void testRectConstructor() 2. Rect r1 = new Rect(100, 200, 50, 60);3. System.out.println(r1);4. 在main方法中调用测试方法, 测试对象的创建:1. public static void main(String args) 2. testR

22、ectConstructor();3. 测试结果如下, 说明构造器能够创建位置在(100, 200)大小是50X60的矩形:1. x=100, y=200, width= 50, height=60完整的代码参考:1. public class Rect 2. int x;3. int y;4. int width;5. int height;6. 7. public Rect(int x, int y, int width, int height) 8. this.x = x;9. this.y = y;10. this.width = width;11. this.height = hei

23、ght;12. 13. 14. public void setLocation(int x, int y) 15. this.x = x;16. this.y = y;17. 18. 19. public void setSize(int width, int height) 20. this.width = width;21. this.height = height;22. 23. 24. public String toString() 25. return x= + x + , y= + y + , width= + width + , height=26. + height;27.

24、28. 29. 30. public class TestRect 31. 32. public static void main(String args) 33. testRectConstructor();34. 35. 36. public static void testRectConstructor() 37. Rect r1 = new Rect(100, 200, 50, 60);38. System.out.println(r1);39. 40. 41. 3.4 扩展利用重载构造器便捷创建正方形, 和创建在原点的矩形.在Rect类中添加重载构造器用于创建正方形, 构造器中使用t

25、his() 调用本类的构造器, 可以重用构造器代码:1. public Rect(int x, int y, int size) 2. this(x, y, size, size);3. 4. 5. public Rect(int width, int height) 6. this( 0, 0, width, height);7. 更新测试方法, 增加测试代码:1. public static void testRectConstructor() 2. Rect r1 = new Rect(100, 200, 50, 60);3. System.out.println(r1);4. /*5.

26、 * 扩展练习 创建大小为100X200的矩形6. */7. Rect r2 = new Rect(100, 200);8. System.out.println(r2);9. /*10. * 扩展练习 创建位置在(100, 200)大小为30X30的正方形11. */12. Rect r3 = new Rect(100, 200, 30);13. System.out.println(r3);14. 执行测试结果为:1. x=100, y=200, width= 50, height=602. x=0, y=0, width= 100, height=2003. x=100, y=200, width= 30, height=30

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1