首页 > 解决方案 > 如何为 OOP Java 中的 main 方法编写代码?

问题描述

所以,这是我的任务。

您只需使用屏幕上的一个点,以及屏幕上的一个圆。让我们将它们视为游戏中的角色。我们将把这两个角色都称为可移动对象。如果对象可以在屏幕上移动,则称它们是可移动的。允许以下简单动作:

-公共无效moveLeft();// 将 x 向左移动 1 个单位

-public void moveRight(); // 将 x 向右移动 1 个单位

-公共无效moveUp();// 向上移动 y 1 个单位

-公共无效moveDown();// 将 y 向下移动 1 个单位

-公共无效显示();// 打印所有关于 Movable 的信息

Point 和 Circle 字符都是可移动的。Point 具有 x 坐标和 ay 坐标。Circle 有一个 Point(代表它的中心)和一个半径(为了我们的目的,这是一个 int)。属性可以全部设置为公共。

- 不用说,Point 和 Circle 应该实现 Movable 的所有方法,因为它们是 Movable 对象。

-角色要移动的屏幕是一个二维屏幕,其左上角为坐标(0,0),右下角为坐标(200,200)。

- 确保当角色在屏幕上移动时,他们不会离开屏幕,尤其是圆形角色,因为它涉及到半径。

- 对于 display() 函数,在 Point 中,打印 Point: (x,y),在 Circle 中,打印 Circle: (x,y), r。

输入格式 第一个输入是 1(点)或 2(圆)。如果是Point,后面跟着2个非负整数,如果是Circle,后面跟着3个非负整数,第3个是半径。然后读取一个正整数 m,表示必须执行的操作数,然后是操作本身。- 1 - moveLeft() - 2 - moveRight() - 3 - moveUp() - 4 - moveDown()

输入样本

1

0 0

5

4

4

2

2 

1

输出样本

Point:·(1,2)

这缺少主要方法,但是我又不知道如何为它编码。我对 main 方法的编码很难理解,你能帮我吗?

    interface Moveable
{
   public void moveLeft();
   public void moveRight();
   public void moveUp();
   public void moveDown();
   public void display();
}

class Point implements Moveable

{

public static final int MAX_CHAR=200;
protected int x;
protected int y;

public Point(int X, int Y) { x=X; y=Y; }
public int GetX() { return(x); }
public int GetY() { return(y); }


Point(Point p) { this.x = p.x; this.y=p.y; } //copy constructor

//implements Moveable

public void moveLeft()
{
    if (x>0)
{
    x--;
}

else{

System.out.println("BUMP:Bounces off left side");

}

}

public void moveRight()
{

    if (x<Point.MAX_CHAR)

{
    x++;
}

else
{
    System.out.println("BUMP:Bounces off right side");
}

}
public void moveUp()
{
if (y>0)
{
    y--;
}

else

{
    System.out.println(" BUMP: Bounces off top ");
}
}
public void moveDown()
{
if (y<Point.MAX_CHAR)
{
    y++;
}
else
{
    System.out.println(" BUMP: Bounces off bottom");
}

}
@Override public String toString()

{

return(
new String("Point:(" +x + "," +y+")")

);

}

public void display()

{

System.out.println(toString());

}

}

/*****************************************************************************/

class Point implements Moveable

{

public static final int MAX_CHAR=200;
protected int x;
protected int y;

public Point(int X, int Y) { x=X; y=Y; }
public int GetX() { return(x); }
public int GetY() { return(y); }

Point(Point p) { this.x = p.x; this.y=p.y; } //copy constructor

//implements Moveable

public void moveLeft()

{

    if (x>0)

{

    x--;

}

else
{
    System.out.println("BUMP:Bounces off left side");

}

}

public void moveRight()

{

if (x<Point.MAX_CHAR)
{

    x++;
}
else
{
    System.out.println("BUMP:Bounces off right side");
}

}

public void moveUp()

{
if (y>0)
{
    y--;

}

else

{
    System.out.println(" BUMP: Bounces off top ");

}

}

public void moveDown()

{
if (y<Point.MAX_CHAR)
{
    y++;
}

else
{

    System.out.println(" BUMP: Bounces off bottom");

}

}



@Override public String toString()

{

return(

new String("Point:(" +x + "," +y+")")

);

}

public void display()

{

System.out.println(toString());

}
}

/******************************************************************/

class Point implements Moveable

{



public static final int MAX_CHAR=200;
protected int x;
protected int y;

public Point(int X, int Y) { x=X; y=Y; }
public int GetX() { return(x); }
public int GetY() { return(y); }


Point(Point p) { this.x = p.x; this.y=p.y; } //copy constructor



//implements Moveable

public void moveLeft()

{

if (x>0)

{

    x--;

}

else

{

System.out.println("BUMP:Bounces off left side");

}

}



public void moveRight()

{

if (x<Point.MAX_CHAR)

{

x++;

}

else

{

System.out.println("BUMP:Bounces off right side");

}

}



public void moveUp()

{

if (y>0)

{
    y--;
}

else

{
System.out.println(" BUMP: Bounces off top ");

}

}


public void moveDown()

{
if (y<Point.MAX_CHAR)
{
    y++;

}

else
{
    System.out.println(" BUMP: Bounces off bottom");
}

}

@Override public String toString()

{

return (new String("Point:(" +x + "," +y+")"));

}

public void display()

{

System.out.println(toString());

}
}

标签: javaencapsulation

解决方案


推荐阅读