我正在开发这个程序,在游戏结束时,我会问用户是否想再玩一次。如果他们同意了,我需要开始一个新的游戏。我创建了一个restart()方法:
public void restart(){
Game g = new Game();
g.playGame();
}
然而,当我调用这个方法时,我的程序中的一些值仍然是它们在前一个游戏中的值。
有没有一个游戏可以清除所有的东西,然后用所有的默认值创建一个新的游戏实例?
我有一个具有以下模式的类
class MyClass
{
const x = 'abc';
const y = '123';
function _contruct() {}
}
有没有办法让常量在类体中保持不设置,并在构造函数被调用后动态设置?例如,如下所示:
class MyClass
{
const x;
const y;
function _contruct()
{
$this->setStuff();
}
function setStuff()
{
$this-
我正在使用以下命令创建一个Python程序: def arena(type = 'c'):
if type == 'c':
#do some stuff
elif type == 'd':
#do other stuff
else:
#do different stuff 然而,我很懒,我更喜欢在交互式外壳中输入arena(c)而不是arena('c')来调用函数操作。有没有办法做到这一点?多么? 我之前只用了0,1,2,但让它用字母更容易记住。
class Dad
{
protected static String me = "dad";
public void printMe()
{
System.out.println(me);
}
}
class Son extends Dad
{
protected static String me = "son";
}
public void doIt()
{
new Son().printMe();
}
函数doIt将打印"dad“。有没有办法让它打印“儿子”?
有没有办法重置函数中声明为静态的变量?这样做的目的是确保调用函数时不会有来自不相关调用的延迟值。例如,我有一个对矩阵的列进行运算的函数。
int foo(matrix *A, int colnum, int rownum){
static int whichColumn;
static int *v; //vector of length A->nrows
if (column != whichColumn){
memset(v,0,size);
whichColumn = which;
}
//do other things
}
该函数被调用n次,
当被子类调用时,我试图让一个超类方法在子类方法/字段上运行。例如
public class CoffeeMachine() {
protected int cost = 3;
protected int getCost() {
return cost;
}
}
public class FancyCoffeeMachine() extends CoffeeMachine{
protected int cost = 6;
}
main{
FancyCoffeeMachine expensive = new FancyCoffeeMac
我有一个.on()函数,如下所示:
$('#gallery').on('click', '.gon', function() {
countn = ($(this).data("click_count") || 0) + 1;
$(this).data("click_count", countn);
// set up for first move
if ( countn == 1 ) {
do something........
countn是计算点击次数的
我的代码是这样的:
<?
$a="localhost";
function body(){
global $a;
echo $a;
}
function head(){
global $a;
echo $a;
}
function footer(){
global $a;
echo $a;
}
?>
有没有办法在一个地方定义全局变量,并同时在所有函数中访问变量$a?而不用更多地使用global $a;?
可以向匿名类传递参数或访问外部参数吗?例如:
int myVariable = 1;
myButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// How would one access myVariable here?
}
});
有没有办法让侦听器访问myVariable或传递myVariable,而不将侦听器创建为实际的命名类?