JAVA面向对象.docx
《JAVA面向对象.docx》由会员分享,可在线阅读,更多相关《JAVA面向对象.docx(16页珍藏版)》请在冰豆网上搜索。
JAVA面向对象
JavaOOPDay01
Top
1.移方块游戏——定义Rect类,编写测试方法testNew
2.移方块游戏——构建窗体,设置大小和位置
3.移方块游戏——添加Rect重载的构造方法
1移方块游戏——定义Rect类,编写测试方法testNew
1.1问题
如何用Java代码表达移方块游戏中的矩形个体,且矩形对象要包含矩形的位置和大小特征,如图-1(途中绿色方块是要被移动的矩形)所示。
图-1
1.2方案
计算机的绘图坐标系:
以矩形区域的左上角为原点(O),原点开始由左至右为X轴方向,原点开始由上至下为Y轴.与数学平面坐标系不同:
Y轴正方向相反.
在计算机绘图坐标系中可以用4个属性明确定义一个矩形,分别是:
x,y,width,height.
如图-2说明,绘图坐标系与矩形个体的关系:
图-2
解决方案:
创建Java类Rect,包含x,y,width,height属性,为Rect增加修改位置的方法setLocation()和修改大小的方法setSize().
创建测试类TestRect,添加测试方法testNew()用于测试对象的创建,和属性的访问.
1.3实现
在Eclipse中创建类Rect表示矩形类型:
1.publicclassRect{
2.//TODO添加类的成员
3.}
在Rect类中添加属性:
x,y,width,height
1.intx;
2.inty;
3.intwidth;
4.intheight;
在Rect类添加修改位置的方法:
1. publicvoidsetLocation(intx1,inty1){
2. x=x1;
3. y=y1;
4. }
在Rect类中添加修改大小的方法:
1. publicvoidsetSize(intw,inth){
2. width=w;
3. height=h;
4. }
在Rect类中增加toString()方法用于输出对象的文本描述,方便调试输出:
1. publicStringtoString(){
2. return"x="+x+",y="+y+",width="+width+",height="
3. +height;
4. }
创建测试类TestRect,包含主方法
1.publicclassTestRect{
2.publicstaticvoidmain(String[]args){
3.//TODO添加测试代码
4.}
5.}
在测试类testRect添加测试代方法:
testNew()用于测试对象的创建
1. publicstaticvoidtestNew(){
2. Rectr1=newRect();
3. Rectr2=newRect();
4. r1.x=100;
5. r1.y=200;
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.publicstaticvoidmain(String[]args){
2.testNew();
3.}
执行得到测试结果:
1.x=100,y=200,width=50,height=60
2.x=300,y=400,width=60,height=80
完成代码参考:
1./**矩形类型Rect.java*/
2.publicclassRect{
3. intx;
4. inty;
5. intwidth;
6. intheight;
7.
8. publicvoidsetLocation(intx1,inty1){
9. x=x1;
10. y=y1;
11. }
12.
13. publicvoidsetSize(intw,inth){
14. width=w;
15. height=h;
16. }
17.
18. publicStringtoString(){
19. return"x="+x+",y="+y+",width="+width+",height="
20. +height;
21. }
22.}
23./**测试类TestRect.java*/
24.publicclassTestRect{
25.
26. publicstaticvoidmain(String[]args){
27. testNew();
28. }
29.
30. publicstaticvoidtestNew(){
31. Rectr1=newRect();
32. Rectr2=newRect();
33. r1.x=100;
34. 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运算创建的对象,是在不同物理地址上分配的物理对象,引用的值是对象首地址的值,即便属性值相同的对象,也是不同的物理对象,使用==运算只能判断引用值是否相等,只能判断是否是同一个物理位置对象.不能用于判断对象属性是否相等.
两个引用变量可以引用同一个对象.
引用变量可以赋值,赋值以后两个引用变量的值相同,是同一个物理对象首地址值.
在TestRect中添加测试方法:
1.publicstaticvoidtestEquals(){
2. Rectr1=newRect();
3. r1.x=100;
4. r1.y=200;
5. System.out.println(r1);
6.
7. Rectr2=newRect();
8. r2.x=100;
9. r2.y=200;
10. System.out.println(r2);
11.
12. System.out.println(r1==r2);
13.
14. Rectr3=r1;
15. r3.x=300;
16. r3.y=400;
17. System.out.println(r3);
18. System.out.println(r1);
19.
20. }
修改main方法,添加语句调用测试方法testEquals():
1. publicstaticvoidmain(String[]args){
2. //testNew();
3. testEquals();
4. }
执行得到测试结果:
1.x=100,y=200,width=0,height=0
2.x=100,y=200,width=0,height=0
3.false
4.x=300,y=400,width=0,height=0
5.x=300,y=400,width=0,height=0
2移方块游戏——构建窗体,设置大小和位置
2.1问题
在系统中显示一个窗口,并且控制窗口的显示位置,如图-3所示(显示两个不同位置的窗口)。
图-3
2.2方案
利用Java系统SwingAPI创建并且显示窗口,控制窗口的位置.如图4窗口的位置为Location是坐标(x,y),窗口还有位置属性size包含两个值width和height.
图4
SwingAPI类JFrame代表系统的窗口框,在系统类库javax.swing包中需要使用import语句导入到代码中使用:
1.importjavax.swing.JFrame;
JFrame是类,代表窗口框类型,创建为对象代表一个窗口实例:
1.JFrameframe=newJFrame();
JFrame提供了API方法用于控制窗口的位置和大小,以及设置窗口的标题内容:
1. frame.setSize(500,600);
2. frame.setTitle("TestFrame");
3. frame.setLocation(100,100);
在桌面上显示一个窗口使用setVisible(true)方法,如果参数是false就是关闭一个窗口对象.
1. frame.setVisible(true);
使用如下代码可以实现,在关闭窗口时候同时退出应用程序.
1. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
2.3实现
创建窗口测试类TestJFrame,包含main方法用于启动测试案例.
1.publicclassTestJFrame{
2. publicstaticvoidmain(String[]args){
3. //TODO添加测试方法调用
4. }
5.}
在类的声明之前添加import语句导入SwingAPIJFrame类:
1.importjavax.swing.JFrame;
在测试类TestJFrame中增加测试方法创建窗口对象设置属性并且显示:
1. publicstaticvoidtestSizeAndLocation(){
2. JFrameframe=newJFrame();
3. frame.setSize(500,600);
4. frame.setTitle("TestFrame");
5. frame.setLocation(100,100);
6. frame.setVisible(true);
7. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
8. }
在main方法中添加调用测试方法语句,调用测定方法:
1.publicclassTestJFrame{
2. publicstaticvoidmain(String[]args){
3. testSizeAndLocation();
4. }
5.}
执行测试类TestJFrame得到测试结果,测试结果如图-5所示。
图-5
完成代码参考:
1.importjavax.swing.JFrame;
2.
3.publicclassTestJFrame{
4. publicstaticvoidmain(String[]args){
5. testSizeAndLocation();
6. testSizeAndCenter();
7. }
8.
9. publicstaticvoidtestSizeAndLocation(){
10. JFrameframe=newJFrame();
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扩展
根据屏幕和窗口的大小计算并且设置窗口的位置,实现窗口的居中显示,计算原理如图-6所示。
图-6
利用Swing的工具类可以获得当前系统的屏幕显示大小属性.返回值是Dimension类型对象screen,包含两个屏幕大小属性width,height:
1.Dimensionscreen=Toolkit.getDefaultToolkit().getScreenSize();
在测试类中增加居中测试方法testSizeAndCenter()
1. publicstaticvoidtestSizeAndCenter(){
2. JFrameframe=newJFrame();
3. frame.setSize(500,600);
4. frame.setTitle("TestFrame");
5. Dimensionscreen=
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. }
在main方法中调用居中测试方法,观察测试结果:
1.publicstaticvoidmain(String[]args){
2. testSizeAndLocation();
3. testSizeAndCenter();
4. }
执行结果,出现居中的窗口,如图7:
图7
3移方块游戏——添加Rect重载的构造方法
3.1问题
创建矩形实现和初始化矩形对象属性的代码过于复杂庸长:
1.Rectr1=newRect();
2.r1.x=100;
3.r1.y=200;
4.r1.width=200;
5.r1.height=200;
6.
7.Rectr2=newRect();
8.r2.x=100;
9.r2.y=200;
10.r2.width=200;
11.r2.height=200;
要提供更加便捷的创建对象实例的方法.并且要提供多种便捷的创建方式方法.
3.2方案
提供更加便捷的创建矩形类的构造方法,简化矩形对象的创建过程,利用重载的构造器方便构建方式.
3.3实现
重构Rect类增构造器,用于创建并初始化对象,构造器的名字要与类名一致(包括大小写也是一致的),构造器不能声明返回值,构造器中的语句用于初始化对象的属性值:
1. publicRect(intx,inty,intwidth,intheight){
2. this.x=x;
3. this.y=y;
4. this.width=width;
5. this.height=height;
6. }
重构TestRect类增加构造器测试方法,调用构造器初始化并创建对象:
1. publicstaticvoidtestRectConstructor(){
2. Rectr1=newRect(100,200,50,60);
3. System.out.println(r1);
4.}
在main方法中调用测试方法,测试对象的创建:
1. publicstaticvoidmain(String[]args){
2. testRectConstructor();
3. }
测试结果如下,说明构造器能够创建位置在(100,200)大小是50X60的矩形:
1.x=100,y=200,width=50,height=60
完整的代码参考:
1.publicclassRect{
2. intx;
3. inty;
4. intwidth;
5. intheight;
6.
7. publicRect(intx,inty,intwidth,intheight){
8. this.x=x;
9. this.y=y;
10. this.width=width;
11. this.height=height;
12. }
13.
14. publicvoidsetLocation(intx,inty){
15. this.x=x;
16. this.y=y;
17. }
18.
19. publicvoidsetSize(intwidth,intheight){
20. this.width=width;
21. this.height=height;
22. }
23.
24. publicStringtoString(){
25. return"x="+x+",y="+y+",width="+width+",height="
26. +height;
27. }
28.}
29.
30.publicclassTestRect{
31.
32. publicstaticvoidmain(String[]args){
33. testRectConstructor();
34. }
35.
36. publicstaticvoidtestRectConstructor(){
37. Rectr1=newRect(100,200,50,60);
38. System.out.println(r1);
39. }
40.
41.}
3.4扩展
利用重载构造器便捷创建正方形,和创建在原点的矩形.
在Rect类中添加重载构造器用于创建正方形,构造器中使用this()调用本类的构造器,可以重用构造器代码:
1. publicRect(intx,inty,intsize){
2. this(x,y,size,size);
3. }
4.
5. publicRect(intwidth,intheight){
6. this(0,0,width,height);
7. }
更新测试方法,增加测试代码:
1. publicstaticvoidtestRectConstructor(){
2. Rectr1=newRect(100,200,50,60);
3. System.out.println(r1);
4. /*
5. *扩展练习创建大小为100X200的矩形
6. */
7. Rectr2=newRect(100,200);
8. System.out.println(r2);
9. /*
10. *扩展练习创建位置在(100,200)大小为30X30的正方形
11. */
12. Rectr3=newRect(100,200,30);
13. System.out.println(r3);
14. }
执行测试结果为:
1.x=100,y=200,width=50,height=60
2.x=0,y=0,width=100,height=200
3.x=100,y=200,width=30,height=30