gpt4 book ai didi

java - 如何在 JavaScript 中实现这个 java 模式(使用继承)?

转载 作者:行者123 更新时间:2023-12-01 14:19:53 24 4
gpt4 key购买 nike

这里是工作java代码,为各种游戏使用的许多引擎实现提供处理平衡监听器注册的基本引擎类。例如将会有一个演示引擎,它维护演示游戏的演示平衡,以及同一引擎的现金版本,它从后台等处获取平衡。这里的关键不是实际的java,而是如何实现这种模式在 JavaScript 中。我已经尝试了大约 30 种不同的方法来做到这一点,包括使用 John Resigs 的“简单 JavaScript 继承”和“JavaScript:权威指南”中定义的扩展()糖,使用各种模块模式,使用 that=this 等。解决了这个问题。

这是工作的java代码:

文件引擎.java:

package com.test;
public abstract class Engine {
BalanceListener externalBalanceListener = null;
double balance = 0;
public void registerBalanceListener(BalanceListener balanceListener) {
externalBalanceListener = balanceListener;
balanceListener.update(balance); // call once when first register
}
public double getBalance() {
return balance;
}
protected void setBalance(double newBal) {
if (newBal != balance) {
balance = newBal;
if (externalBalanceListener != null) {
externalBalanceListener.update(newBal);
}
}
}
public abstract double startGame(double stake, int numLines);
}

文件 BalanceListener.java

package com.test;
public interface BalanceListener {
void update(double balance);
}

文件 DemoEngine.java

package com.test;
import java.util.Random;

public class DemoEngine extends Engine {
public DemoEngine() {
setBalance(10000);
}
public double startGame(double stake, int numLines) {
double wonAmount;
Random random = new Random();

setBalance (getBalance() - (stake * numLines));

// some game logic
wonAmount = Math.round((random.nextDouble() * 10)) * stake;
setBalance (getBalance() + wonAmount);
return wonAmount;
}
}

文件 DemoGame.java

package com.test;

public class DemoGame {

public class MyListener implements BalanceListener {
public MyListener(){
}
public void update(double balance) {
System.out.println("new balance: " + balance);
}
}

public static void main(String[] args) {
Engine engine = new DemoEngine();

DemoGame demoGame = new DemoGame();

BalanceListener balanceListener = demoGame.new MyListener();

engine.registerBalanceListener(balanceListener);

engine.startGame(10, 20);
}
}

这是一个简单的(失败的)尝试,让同样的事情在 JavaScript 中工作(参见 http://jsfiddle.net/fmX67/ )

function Engine() {
this.balance = 0;
this.externalBalanceListener;

this.registerBalanceListener = function(l) {
this.externalBalanceListener= l;
this.externalBalanceListener(this.balance);
};

this.getBalance = function() {
return this.balance;
};

this.setBalance = function (newBal) {
if (newBal != this.balance) {
this.balance = newBal;
if (this.externalBalanceListener != undefined) {
this.externalBalanceListener(newBal);
}
}
};

};

function DemoEngine() {
this.startGame = function(stake, numLines) {
var won;

setBalance(this.getBalance() - stake*numlines);
won = Math.round(Math.random() * 10) * Stake;

this.setBalance(this.getBalance() + won);

return won;
};
}

DemoEngine.prototype = Engine;

function DemoGame() {

function balanceListener(balance) {
console.log(balance);
}

var engine = new DemoEngine();

engine.registerBalanceListener(balanceListener); // This throws an exception: Uncaught TypeError: Object [object Object] has no method 'registerBalanceListener'

engine.startGame(10, 25);
}

var game = new DemoGame();

显然我不知道我在做什么(尽管读了几本 JS 书籍)。我假设我可以使用组合而不是尝试继承,但这限制了语言的使用以及可以实现的模式。

编辑:这是包含 Shaun West 答案的工作版本。请参阅http://jsfiddle.net/fmX67/3/

function Engine() {
this.balance = 0;
this.externalBalanceListener;

this.registerBalanceListener = function(l) {
this.externalBalanceListener= l;
this.externalBalanceListener(this.balance);
};

this.getBalance = function() {
return this.balance;
};

this.setBalance = function (newBal) {
if (newBal != this.balance) {
this.balance = newBal;
if (this.externalBalanceListener != undefined) {
this.externalBalanceListener(newBal);
}
}
};

};

function DemoEngine() {
this.setBalance(1000);
this.startGame = function(stake, numLines) {
var won;

this.setBalance(this.getBalance() - stake*numLines);
won = Math.round(Math.random() * 10) * stake;

this.setBalance(this.getBalance() + won);

return won;
};
}

DemoEngine.prototype = new Engine();

function DemoGame() {

function balanceListener(balance) {
console.log(balance);
}

var engine = new DemoEngine();

engine.registerBalanceListener(balanceListener); // This throws an exception: Uncaught TypeError: Object [object Object] has no method 'registerBalanceListener'

engine.startGame(10, 25);
}


var game = new DemoGame();

最佳答案

尽管对于来自 Java 的人来说这可能看起来很奇怪,但在您的尝试中您将需要更改此设置:

DemoEngine.prototype = Engine;

对此:

DemoEngine.prototype = new Engine();

如果您想了解更多信息,这个答案非常好:What is the 'new' keyword in JavaScript?

关于java - 如何在 JavaScript 中实现这个 java 模式(使用继承)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17703005/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com