![](/img/trans.png)
[英]Should a (java/android) class that has a listner implemented in the constructor have static methods which are somehow related to the listner actions?
[英]Which actions can I do in implemented methods?
我想模拟射击。 该枪有6发子弹。 每次射击后,子弹的数量应减少。
这是代码
public interface Gun {
public void shot();
public void reload();
}
这是主要的:
package com.example;
class HelloCodiva {
public static void main(String[] args) {
Pistol pistol = new Pistol();
Gun gun;
gun = new Pistol();
gun.shot();
System.out.println(pistol.getBullets());
gun.shot();
gun.shot();
}
}
class Pistol implements Gun {
private int bullets;
private int damage;
private boolean reload;
public Pistol () {
this.bullets = 6;
this.damage = 10;
}
@Override
public void shot() {
this.bullets-=1;
System.out.println("Shotting");
}
@Override
public void reload() {
if(reload){
System.out.println("Reloading...");
reload = false;
}
}
public int getBullets() {
return bullets;
}
}
但我总是得到相同的初始金额(6)。 我做错了什么?
你是从
Gun gun;
gun = new Pistol();
但是算上子弹
Pistol pistol = new Pistol();
System.out.println(pistol.getBullets());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.