如何实现和使用它

时间:2015-12-09 03:02:56

标签: java abstract-class

如何在另一个抽象类中使用这些方法之一?我很少使用抽象类,因为我更喜欢接口。

 package hw3;

    import java.awt.Graphics;
    import java.awt.Rectangle;

    /**
     * Base class for simple objects in a video game.  
     */
    public abstract class Sprite
    {
      /**
       * Horizontal coordinate of the upper-left corner.
       */
      private double x;

      /**
       * Vertical coordinate of the upper-left corner.
       */
      private double y;

      /**
       * Width of this object, normally assumed to be in pixels.
       */
      private int width;

      /**
       * Height of this object, normally assumed to be in pixels.
       */
      private int height;

      /**
       * Flag indicating whether this object has been marked for deletion.
       */
      private boolean delete;

      /**
       * Number of times the update() method has been called.
       */
      private int ticks;

      /**
       * A Renderer for drawing this object using a graphics context.
       */
      private Renderer renderer;

      /**
       * Constructs a new Sprite.
       * @param x
       * @param y
       * @param width
       * @param height
       */

      protected Sprite(double x, double y, int width, int height, Renderer givenRenderer)
      {
        this.x = x;
        this.y = y;
        this.width = width;
        this.height = height;
        renderer = givenRenderer;
      }

      /**
       * Returns the x-coordinate rounded to an integer.
       * @return
       *   x-coordinate rounded to an integer
       */
      public int getX()
      {
        return (int) Math.round(x);
      }

      /**
       * Returns the y-coordinate rounded to an integer.
       * @return
       *   y-coordinate rounded to an integer
       */
      public int getY()
      {
        return (int) Math.round(y);
      }

      /**
       * Returns the x-coordinate's exact value.
       * @return
       *   the x-coordinate
       */
      public double getXExact()
      {
        return x;
      }

      /**
       * Returns the y-coordinate's exact value.
       * @return
       *   the y-coordinate
       */
      public double getYExact()
      {
        return y;
      }

      /**
       * Returns the width.
       * @return
       *   width of this object
       */
      public int getWidth()
      {
        return width;
      }

      /**
       * Returns the height.
       * @return
       *   height of this object
       */
      public int getHeight()
      {
        return height;
      }

      /**
       * Returns the bounding rectangle for this object.
       * @return
       *   bounding rectangle 
       */
      public Rectangle getRect()
      {
        return new Rectangle(getX(), getY(), width, height);
      }

      /**
       * Sets the position of this object.
       * @param newX
       *   the new x-coordinate
       * @param newY
       *   the new y-coordiante
       */
      public void setPosition(double newX, double newY)
      {
        x = newX;
        y = newY;
      } 

      /**
       * Returns true if this object has been marked for deletion.
       * @return
       */
      public boolean shouldDelete()
      {
        return delete;
      }

      /**
       * Marks this object for deletion.
       */
      public void markForDeletion()
      {
        delete = true;
      }

      /**
       * Determines whether this object overlaps the given object.
       * @param other
       * @return
       */
      public boolean collides(Sprite other)
      {
         return this.getRect().intersects(other.getRect());
      }

      /**
       * Uses this object's Renderer to draw the object.
       * @param g
       *   graphics context for rendering
       */
      public void draw(Graphics g)
      {
        renderer.render(g, this);
      }  

      /**
       * Returns the number of times that update() has been invoked for this
       * object.
       * @return
       *   elapsed ticks
       */
      public int getTicks()
      {
        return ticks;
      }

      /**
       * Updates this object's attributes for the next frame.
       */
      public void update()
      {
        ticks += 1;
      }
    }

我认为它会像

一样简单
package hw3;

    public class Explosion {

        private int x;

        public Explosion (int x, int y, int width, int height, Renderer r, int initialCount) {
            // TODO Auto-generated constructor stub
        }

        public int getCount(){
            return 0;

        }

        public int getX() extends getX()
        {
            return (int) Math.round(x);
        }

    }

但这会导致爆炸:/

2 个答案:

答案 0 :(得分:0)

也许试试......

public class Explosion extends Sprite {

答案 1 :(得分:0)

您必须使用

扩展抽象类
extends Sprite

示例代码:

abstract class Person {

    String name;
    int age;
    public Person(String name, int age) {
        super();
        this.name = name;
        this.age = age;
    }
}


class Employee extends Person {

    public Employee(String name, int age) {
        //Explicitly call super constructor Person(String,int).
        //The parameters in constructor of your parent Person is required
        //hence you have to comply by passing the variable that you will ask to 
        //the client of your Employee Class
        super(name, age);

    }
}