我正在尝试这样我可以在父类(img,LocX,LocY)中有三个值,它们应该能够根据它们所在的子类而改变。
我想拥有它当我尝试使用该方法绘制播放器时,它会绘制播放器。但是我希望能够使用相同的方法在敌人用它来吸引敌人时绘制它。我需要这样做,因为我需要使用继承和多态。
我没有收到任何语法错误,但是当我尝试运行游戏时遇到错误(在我开始尝试这样做之前它正在工作)。以下是父类和子类的位置代码:
echo $HOSTNAME
以下是执行所有绘图的JPanel的代码:
public abstract class Entity { //hold things such as player, and values realted to that specific entity (nice especially for organization purposes), both player and enemy the same health and speed, however they have different images that go along with them
protected MazeModel model = new MazeModel();
protected Image i = new Image();
//both enemy and player have images, and have locations, so it was easy to get both inheritance and polymorphism in this one if they shared an abstract image, and set of coordinates
abstract BufferedImage img(); //was looking at ways to extend a single BufferedImage to multiple classes for inheritance, I ended up finding out that they way to do it also covers polymorphism (originally did not have an idea for how to apply polymorphism)
abstract int LocX();
abstract int LocY();
protected BufferedImage image; //they all have images, however they are different in each class
//these are what is in common between player and enemy, these are currently not in use, however it is still inheritism
protected int health = 100; //these two are not implemented yet, but are there to show inheritance
protected int speed = 50;
}
class Player extends Entity{ //Inheritance
BufferedImage img(){
return i.getPlayer(); //img is the player's image in this case
}
int LocX(){
return model.getPlayerX(); //gets the player's x
}
int LocY(){
return model.getPlayerY(); //gets the player's y
}
private int playerHealth = health;
private int playerSpeed = speed;
//setters and getters to use the player's values
public void setPlayerHealth(int playerHealth){
this.playerHealth = playerHealth;
}
public int getPlayerHealth(){
return playerHealth;
}
//
public Player(MazeModel model){ //constructor
this.model = model;
}
private BufferedImage image = i.getPlayer();
//for setting the player's initial starting point
public void setPlayerStart(int x, int y){
model.setPlayerX(x); //sets the starting x position of the player's image
model.setPlayerY(y); //sets the starting y position of the player's image
System.out.println("Your current coordinates are " + model.getPlayerX() + ", " + model.getPlayerY());
}
//for drawing the player
public void paintImg(Entity e, Graphics g){ //polymorphism
g.drawImage(e.img(), e.LocX(), e.LocY(), null);
}
public void playerPaint(Graphics g){
g.drawImage(image, model.getPlayerX(), model.getPlayerY(), null); //creates the user
}
}
//these next two I will possibly add later if I have the time
class Enemy extends Entity{
BufferedImage img(){
return i.getEnemy();
}
int LocX(){
return model.getEnemyX(); //gets the player's x
}
int LocY(){
return model.getEnemyY(); //gets the player's y
}
private int enemyHealth = health;
private int enemySpeed = speed;
private int enemyX = 400;
private int enemyY = 50;
private BufferedImage image = i.getEnemy();
public void paintImg(Entity e, Graphics g){
g.drawImage(e.img(), e.LocX(), e.LocY(), null);
}
public void paintEnemy(Graphics g){
g.drawImage(image, model.getEnemyX(), model.getEnemyY(), null); //enemies location, fixed position for now
}
}
我相信我接近这个,但由于某种原因,我无法弄清楚到底出了什么问题。我希望能够这样做,因为我需要以某种方式同时使用继承和多态。
答案 0 :(得分:1)
播放器中的内容看起来有问题:
//for drawing the player
public void paintImg(Entity e, Graphics g){ //polymorphism
g.drawImage(e.img(), e.LocX(), e.LocY(), null);
}
public void playerPaint(Graphics g){
g.drawImage(image, model.getPlayerX(), model.getPlayerY(), null); //creates the user
}
第一种方法,paintImg不使用require对象的任何状态,也不改变状态,并且可以是静态的。我不确定为什么你将它评论为多态,因为多态性它绝对不是。
实际上我将合并上面的两个方法并将它们放在Entity类中。要获得多态性,必须继承方法,而您的Entity类没有。我创建了一个方法,paintImg或更好的`paintEntity,让它使用Graphics参数和它自己的状态来绘制自己,然后让你的其他对象继承这个方法并在需要时修改它。