How can I make an abstract class that will implement their methods and create 2 or more objects that need to be added to the list later? Do not paint with words how to do it, in theory I know, in practice I can not implement it! To whom it is not difficult, chew on a dumb student every detail, for how much I have not tried, only mistakes come out.

For example, here is the first class:

import java.awt.Color; import java.awt.Graphics; import java.awt.Rectangle; public class BasicEnemy extends GameObject { private Handler handler; public BasicEnemy(float x, float y, ID id, Handler handler) { super(x, y, id); velX = 5; velY = 5; this.handler = handler; } public Rectangle getBounds(){ return new Rectangle((int)x,(int) y, 16, 16); } public void tick() { x += velX; y += velY; if(y <= 0 || y >= Game.HEIGHT - 48) velY *= -1; if(x <= 0 || x >= Game.WIDTH - 20) velX *= -1; handler.addObject(new Trail(x, y, ID.Trail, Color.red, 16, 16, 0.02f, handler)); } public void render(Graphics g) { g.setColor(Color.red); g.fillRect((int)x, (int)y, 16, 16); } } 

And the second:

 import java.awt.Color; import java.awt.Graphics; import java.awt.Rectangle; public class SmartEnemy extends GameObject { private Handler handler; private GameObject player; public SmartEnemy(int x, int y, ID id, Handler handler) { super(x, y, id); this.handler = handler; for (int i = 0; i < handler.object.size(); i++){ if(handler.object.get(i).getId() == ID.Player) player = handler.object.get(i); } } public Rectangle getBounds(){ return new Rectangle((int)x, (int)y, 16, 16); } public void tick() { x += velX; y += velY; float diffX = x - player.getX() - 8; float diffY = y - player.getY() - 8; float distance = (float) Math.sqrt((x-player.getX())*(x-player.getX()) + (y - player.getY())*(y-player.getY())); velX = (float) ((-1.0/distance) * diffX); velY = (float) ((-1.0/distance) * diffY); if(y <= 0 || y >= Game.HEIGHT - 48) velY *= -1; if(x <= 0 || x >= Game.WIDTH - 20) velX *= -1; handler.addObject(new Trail(x, y, ID.Trail, Color.green, 16, 16, 0.02f, handler)); } public void render(Graphics g) { g.setColor(Color.green); g.fillRect((int)x, (int)y, 16, 16); } } 

Their methods are the same, we need an abstract class! Previously, they were created in this way:

 handler.addObject(new BasicEnemy(r.nextInt(Game.WIDTH - 50), r.nextInt(Game.HEIGHT - 50), ID.BasicEnemy, 5, 5, handler)); 

The handler class itself, which lists:

 package com.wavegame.main; import java.awt.Graphics; import java.util.LinkedList; public class Handler { LinkedList<GameObject> object = new LinkedList<GameObject>(); public void tick(){ for (int i = 0; i < object.size(); i++){ GameObject tempObject = object.get(i); tempObject.tick(); } } public void render(Graphics g){ for (int i = 0; i < object.size(); i++){ GameObject tempObject = object.get(i); tempObject.render(g); } } public void clearEnemys(){ for (int i = 0; i < object.size(); i++){ GameObject tempObject = object.get(i); object.clear(); if (Game.gameState != Game.STATE.End) addObject(new Player((int)tempObject.getX(), (int)tempObject.getY(), ID.Player, this)); } } public void deleteEnemy(ID id){ for (int i = 0; i < object.size(); i++){ GameObject tempObject = object.get(i); if(tempObject.getId() == id){ removeObject(tempObject); } } } public boolean objectAmountFrom2To4(ID id){ int amount = 0; for (int i = 0; i < object.size(); i++){ GameObject tempObject = object.get(i); if (tempObject.getId() == id){ amount++; } } if (amount > 2 && amount < 4){ return true; } return false; } public boolean checkNotEnough(ID id){ int amount = 0; for (int i = 0; i < object.size(); i++){ GameObject tempObject = object.get(i); if (tempObject.getId() == id){ amount++; } } if (amount < 2){ return true; } return false; } public void addObject(GameObject object){ this.object.add(object); } public void removeObject(GameObject object){ this.object.remove(object); } } 

And the GameObject class that opponents inherit:

 package com.wavegame.main; import java.awt.Graphics; import java.awt.Rectangle; public abstract class GameObject { protected float x, y; protected ID id; protected float velX, velY; public GameObject(float x, float y, ID id){ this.x = x; this.y = y; this.id = id; } public abstract void tick(); public abstract void render(Graphics g); public abstract Rectangle getBounds(); public void setX(int x){ this.x = x; } public void setY(int y){ this.y = y; } public float getX(){ return x; } public float getY(){ return y; } public void setId(ID id){ this.id = id; } public ID getId(){ return id; } public void setVelX(int velX){ this.velX = velX; } public void setVelY(int velY){ this.velY = velY; } public float getVelX(){ return velX; } public float getVelY(){ return velY; } } 

___________________________________________________ UPDT:

In this case, the factory method works great, thanks again for it, but I need to use an abstract class to create objects and add to the gameplay itself, how can I do this? The abstract class has been created, but I don’t see the point of using it here, but I specifically need it, could you describe how to apply it? All data necessary for work is given above.

 import java.awt.Graphics; import java.awt.Rectangle; public abstract class Enemy extends GameObject { //all other enemies extends "Enemies"!!!!!!!!! public Enemy(float x, float y, ID id) { super(x, y, id); } public abstract void tick(); public abstract Rectangle getBounds(); public abstract void render(Graphics g); } 

    1 answer 1

    It is not entirely clear how using an abstract class you want to create objects. He alone is unable to create anything.

    Alternatively, you can use the фабричный метод pattern:

     enum EnemyType { SMART, BASIC } abstract class Enemy extends GameObject { //реализация или определение общих методов } class SmartEnemy extends Enemy { } class BasicEnemy extends Enemy { } final class EnemyFactory { private EnemyFactory() { } public static Enemy create(EnemyType type) { switch (type) { case SMART: return new SmartEnemy(); case BASIC: return new BasicEnemy(); default: throw new IllegalArgumentException("type is wrong"); } } } 
    • Thanks for the template, but I have the problem that when creating an abstract class, I bring the method there: public abstract void render (Graphics g); And when calling this method through an abstract class, for example: AbstractEnemy enemy; enemy.render (here he asks for Graphics g); The compiler curses any argument except null, how to deal with it? - Stab Ones
    • I just need the implementation or definition of common methods, and preferably also how to use them - Stab Ones
    • I do not quite understand, you have a GameObject, do the same with the Enemy class. The benefits of the abstract class and of inheritance in general are to reduce code duplication by moving common methods to the parent class. If you do not have such a code, but you want to do the splitting into entities, then use interfaces. - Artem Konovalov
    • Give the code, with the method not accepting anything except null - Artem Konovalov
    • See update my post. - Stab Ones