我是一名初学Java程序员,并且已经花了很长时间。我需要将下面的程序转换为OOP格式,并且无法在没有错误的情况下进行编译。我想我会发布工作的非格式化程序,而不是我失败和不稳定的尝试。如果有人可以将以下程序转换为OOP,那将非常感激。请原谅任何低效率或邋iness,因为我是新手。
感谢您的帮助
import java.awt.*;
public class Turtle3 {
public static void drawLine(Turtle myrtle, Color color, int x1, int y1, int x2, int y2) {
myrtle.hide();
myrtle.penUp();
myrtle.setPenColor(color);
myrtle.moveTo(x1, y1);
myrtle.penDown();
myrtle.moveTo(x2, y2);
}
public static void setPenWidth(int w) {
World worldObj = new World();
Turtle myrtle = new Turtle(200, 200, worldObj);
Turtle billy = new Turtle(100, 700, worldObj);
Turtle thomas = new Turtle(100, 100, worldObj);
myrtle.setColor(Color.RED);
myrtle.setPenWidth(50);
myrtle.forward(50);
myrtle.show();
billy.setColor(Color.BLUE);
billy.setPenWidth(100);
billy.forward(300);
thomas.show();
thomas.setColor(Color.BLACK);
thomas.setPenWidth(100);
thomas.forward(50);
thomas.show();
};
public static void main(String[] args) {
World worldObj = new World();
Turtle myrtleTheTurtle = new Turtle(0, 0, worldObj);
drawLine(myrtleTheTurtle, Color.RED, 10, 20, 700, 20);
drawLine(myrtleTheTurtle, Color.BLUE, 10, 40, 700, 40);
drawLine(myrtleTheTurtle, Color.BLACK, 10, 60, 700, 60);
drawLine(myrtleTheTurtle, Color.BLACK, 10, 80, 700, 80);
drawLine(myrtleTheTurtle, Color.BLACK, 10, 100, 700, 100);
drawLine(myrtleTheTurtle, Color.RED, 10, 120, 700, 120);
drawLine(myrtleTheTurtle, Color.BLUE, 10, 140, 700, 140);
drawLine(myrtleTheTurtle, Color.BLACK, 10, 160, 700, 160);
drawLine(myrtleTheTurtle, Color.BLACK, 10, 180, 700, 180);
drawLine(myrtleTheTurtle, Color.BLACK, 10, 200, 700, 200);
drawLine(myrtleTheTurtle, Color.RED, 10, 220, 700, 220);
drawLine(myrtleTheTurtle, Color.BLUE, 10, 240, 700, 240);
drawLine(myrtleTheTurtle, Color.RED, 10, 260, 700, 260);
drawLine(myrtleTheTurtle, Color.BLUE, 10, 280, 700, 280);
drawLine(myrtleTheTurtle, Color.BLACK, 10, 300, 700, 300);
drawLine(myrtleTheTurtle, Color.BLACK, 10, 320, 700, 320);
drawLine(myrtleTheTurtle, Color.BLACK, 10, 340, 700, 340);
drawLine(myrtleTheTurtle, Color.RED, 10, 360, 700, 360);
drawLine(myrtleTheTurtle, Color.BLUE, 10, 380, 700, 380);
drawLine(myrtleTheTurtle, Color.BLACK, 10, 400, 700, 400);
drawLine(myrtleTheTurtle, Color.BLACK, 10, 420, 700, 420);
drawLine(myrtleTheTurtle, Color.BLACK, 10, 440, 700, 440);
drawLine(myrtleTheTurtle, Color.BLUE, 10, 460, 700, 460);
drawLine(myrtleTheTurtle, Color.BLACK, 10, 480, 700, 480);
drawLine(myrtleTheTurtle, Color.BLACK, 480, 10, 480, 700);
drawLine(myrtleTheTurtle, Color.BLACK, 180, 10, 180, 700);
drawLine(myrtleTheTurtle, Color.BLACK, 480, 10, 480, 700);
drawLine(myrtleTheTurtle, Color.BLACK, 330, 10, 330, 700);
setPenWidth(50);
}//end of main method
}//end of class
答案 0 :(得分:0)
我不确定你在问什么。您已在setPenWidth()
方法中使用OOP。如果您想在程序中使用更多OOP,您可能希望将方法drawLine()
移至Turtle
类,并将其更改为
public void drawLine(Color color, int x1, int y1, int x2, int y2)
{
this.hide();
this.penUp();
this.setPenColor(color);
this.moveTo(x1, y1);
this.penDown();
this.moveTo(x2, y2);
}
引用Turtle
的当前实例,而不需要将其作为参数传递。注意:您必须取消静态关键字才能生效。然后,您必须更改主要方法中的代码,才能将drawLine()
的调用从drawLine(myrtleTheTurtle, color, x1, y1, x2, y2);
更改为myrtleTheTurtle.drawLine(color, x1, y1, x2, y2)