java - 如何访问由构造函数生成的对象变量
问题描述
我有 2 个按钮,1 个使用 round1rock 中的构造函数类,而其他 1 个尝试访问这些参数。我的代码有什么问题?
构造函数类
public ROCK(int hp, int stamina, int attack, int speed, String type){
this.hp=hp;
this.stamina= stamina;
this.attack= attack;
this.speed = speed;
this.type = type;
}
2个按钮:
private void continueRound1 (ActionEvent event){
ROCK round1Rock= new ROCK( 500, 100, 100, 100, "Metamorphic");
}
private void Attack (ActionEvent event){
round1Rock.hp = 12;
}
我如何访问以前制作的对象?
解决方案
当你定义
private void continueRound1 (ActionEvent event){
ROCK round1Rock= new ROCK( 500, 100, 100, 100, "Metamorphic");
}
您ROCK round1Rock
只是为函数定义continueRound1
。要Attack
访问该对象,您需要round1Rock
在类级别上进行定义。
尝试:
ROCK round1Rock = null;
private void continueRound1 (ActionEvent event){
round1Rock= new ROCK( 500, 100, 100, 100, "Metamorphic");
}
private void Attack (ActionEvent event){
round1Rock.hp = 12;
}
推荐阅读
- spring - heroku 应用程序要求我在没有启用它的情况下登录
- python - 为什么我在尝试删除记录时收到“类 'sqlalchemy.engine.row.Row' 未映射”?
- android - Android Studio 制作的类有错误 - 这里发生了什么?
- html - 使用 HTML 字符串生成默认浏览器
- go - Golang错误UpdateAllTokens和Generate All token不是由包声明的吗?
- javascript - jQuery 事件处理程序不适用于动态插入的表单元素
- mysql - Mysql无法连接数据库服务器
- git - 我有两个本地仓库,一个在学校,一个在家里,还有一个远程仓库在 github,我如何从一个本地仓库获取提交以同步到另一个本地仓库
- reactjs - 将组件作为构造函数传递时如何键入组件?
- android - 中心偏移的drawArc