我的朋友告訴我,我的代碼不是很OO。我將如何使這個更多的OO和可重用? 我試過做一個叫做level的類,它在平面上打印圖像,但是注意到了打印,所以沒有成功。使此代碼更面向對象?
(代碼轉儲到來,我很抱歉)
public class Main extends Applet implements Runnable, KeyListener,
java.awt.event.MouseListener {
double x_pos = 300;
double y_pos = 200;
int radius = 20;
int appletsize_x = 640;
int appletsize_y = 440;
double speed = 3;
float speedModifier = 1f;
Image grass;
Image hero;
Image hero45;
Image hero90;
Image hero135;
Image hero180;
Image hero225;
Image hero270;
Image hero315;
Image sprite;
Image tree;
Image path;
private boolean leftPressed;
private boolean rightPressed;
private boolean downPressed;
private boolean upPressed;
private Image dbImage;
private Graphics dbg;
public void init() {
Dimension dim = getMaximumSize();
this.setSize(appletsize_x, appletsize_y);
MediaTracker mt = new MediaTracker(this);
tree = getImage(getCodeBase(), "tree_64.png");
grass = getImage(getCodeBase(), "grassTile.png");
path = getImage(getCodeBase(), "path.png");
hero = getImage(getCodeBase(), "hero.png");
hero45 = getImage(getCodeBase(), "hero45.png");
hero90 = getImage(getCodeBase(), "hero90.png");
hero135 = getImage(getCodeBase(), "hero135.png");
hero180 = getImage(getCodeBase(), "hero180.png");
hero225 = getImage(getCodeBase(), "hero225.png");
hero270 = getImage(getCodeBase(), "hero270.png");
hero315 = getImage(getCodeBase(), "hero315.png");
sprite = getImage(getCodeBase(), "hero.png");
mt.addImage(hero, 0);
mt.addImage(path, 0);
mt.addImage(tree, 0);
mt.addImage(grass, 0);
mt.addImage(hero45, 0);
mt.addImage(hero90, 0);
mt.addImage(hero135, 0);
mt.addImage(hero180, 0);
mt.addImage(hero225, 0);
mt.addImage(hero270, 0);
try {
mt.waitForID(0);
} catch (InterruptedException ie) {
}
}
public void start() {
this.addKeyListener(this);
this.addMouseListener(this);
Thread th = new Thread(this);
th.start();
}
public void stop() {
}
public void destroy() {
}
public void run() {
// lower ThreadPriority
this.requestFocusInWindow();
Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
while (true) {
if (downPressed && leftPressed) {
double y_decr, x_incr;
y_decr = Math.sqrt(Math.pow(speed, 2))/1.5;
x_incr = Math.sqrt(Math.pow(speed, 2))/1.5;
y_pos += y_decr;
x_pos -= x_incr;
} else if (downPressed && rightPressed) {
y_pos += Math.sqrt(Math.pow(speed, 2))/1.5;
x_pos += Math.sqrt(Math.pow(speed, 2))/1.5;
} else if (upPressed && rightPressed) {
y_pos -= Math.sqrt(Math.pow(speed, 2))/1.5;
x_pos += Math.sqrt(Math.pow(speed, 2))/1.5;
} else if (upPressed && leftPressed) {
y_pos -= Math.sqrt(Math.pow(speed, 2))/1.5;
x_pos -= Math.sqrt(Math.pow(speed, 2))/1.5;
} else {
// Hitting (right)
if (x_pos > this.getSize().width - radius) {
// x_speed = -x_speed;
}
if (leftPressed == true) {
x_pos -= speed * speedModifier;
}
if (rightPressed == true) {
x_pos += speed * speedModifier;
}
if (upPressed == true) {
y_pos -= speed * speedModifier;
}
if (downPressed == true) {
y_pos += speed * speedModifier;
}
}
// Hitting right
if (x_pos > this.getSize().width - 32) {
x_pos = this.getSize().width - 32;
} // Hitting left
if (x_pos < 0) {
x_pos = 0;
}
// } // Hitting top
if (y_pos < 0) {
y_pos = 0;
}
// Hitting bottom
if (y_pos > this.getSize().height - 32) {
y_pos = this.getSize().height - 32;
}
repaint();
try {
// Stop thread for 1 milliseconds
Thread.sleep(20);
} catch (InterruptedException ex) {
// do nothing
}
// set ThreadPriority to maximum value
Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
}
}
public void paint(Graphics g) {
int layout = 0;
int coll = 0;
while (coll <= 440) {
drawRows(layout, grass, g, coll);
coll = coll + 32;
}
drawCollums(0, path, g, 180);
g.drawImage(tree, 50, 40, this);
g.drawImage(tree, 300, 20, this);
g.drawImage(tree, 500, 300, this);
int x_posI = (int) x_pos;
int y_posI = (int) y_pos;
if (downPressed && leftPressed) {
this.sprite = hero225;
} else if (downPressed && rightPressed) {
this.sprite = hero135;
} else if (upPressed && rightPressed) {
this.sprite = hero45;
} else if (upPressed && leftPressed) {
this.sprite = hero315;
} else if (leftPressed == true) {
this.sprite = hero270;
} else if (rightPressed == true) {
this.sprite = hero90;
} else if (upPressed == true) {
this.sprite = hero;
} else if (downPressed == true) {
this.sprite = hero180;
}
// this.sprite will contain value set on last "movement"
g.drawImage(this.sprite, x_posI, y_posI, this);
}
public void update(Graphics g) {
if (dbImage == null) {
dbImage = createImage(this.getSize().width, this.getSize().height);
dbg = dbImage.getGraphics();
}
dbg.setColor(getBackground());
dbg.fillRect(0, 0, this.getSize().width, this.getSize().height);
dbg.setColor(getForeground());
paint(dbg);
g.drawImage(dbImage, 0, 0, this);
}
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_LEFT) {
leftPressed = true;
}
if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
rightPressed = true;
}
if (e.getKeyCode() == KeyEvent.VK_UP) {
upPressed = true;
}
if (e.getKeyCode() == KeyEvent.VK_DOWN) {
downPressed = true;
}
}
@Override
public void keyReleased(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_LEFT) {
leftPressed = false;
}
if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
rightPressed = false;
}
if (e.getKeyCode() == KeyEvent.VK_UP) {
upPressed = false;
}
if (e.getKeyCode() == KeyEvent.VK_DOWN) {
downPressed = false;
}
}
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void mouseClicked(MouseEvent e) {
System.out.println("HIT!");
// TODO Auto-generated method stub
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
public void drawRows(int x, Image image, Graphics g, int coll) {
while (x <= appletsize_x) {
g.drawImage(image, x, coll, this);
x += 32;
}
}
public void drawCollums(int y, Image image, Graphics g, int coll) {
while (y <= appletsize_x) {
g.drawImage(image, coll, y, this);
y += 32;
}
}
}
你可以通過閱讀[神的對象(http://en.wikipedia.org/wiki/God_object)開始,並儘量避免在首位寫他們。這是參考'(代碼轉儲來了,我很抱歉)' – 2011-06-13 17:33:51
我不認爲任何人會重構你的代碼,你甚至沒有說它做了什麼。獲取一本關於面向對象編程的書,從簡單的例子開始嘗試儘可能多地學習。 在這段代碼中實際上沒有/很少的OOP。 – 2011-06-13 17:35:05
要進一步擴展我以前的評論。如果你對每一個不同的職責都有一個單獨的課程,那麼你的代碼就會反映出來,問題也會反映出來。我沒有讀過整個班級,但很明顯,關注的分離會有助於閱讀。 – 2011-06-13 17:38:18