Java 引用另一个类中的方法
我有一个名为Hive的类,其中包含一些实例变量,比如蜂蜜。我有另一个叫做Queen的类,我想使用一个叫做takeHoney()的方法,我在我的hive类中有这个方法,如果可以的话,可以移除2个单位的蜂蜜。我试图做的是向Queen类添加一个构造函数,该构造函数将配置单元作为参数并在本地存储,这样我就可以从Queen访问配置单元中的方法,但它不起作用。有什么问题Java 引用另一个类中的方法,java,methods,constructor,Java,Methods,Constructor,我有一个名为Hive的类,其中包含一些实例变量,比如蜂蜜。我有另一个叫做Queen的类,我想使用一个叫做takeHoney()的方法,我在我的hive类中有这个方法,如果可以的话,可以移除2个单位的蜂蜜。我试图做的是向Queen类添加一个构造函数,该构造函数将配置单元作为参数并在本地存储,这样我就可以从Queen访问配置单元中的方法,但它不起作用。有什么问题 public class Queen extends Bee{ int type = 1; public Queen(
public class Queen extends Bee{
int type = 1;
public Queen(Hive hive){
}
public Queen(int type, int age, int health){
super(type, age, health);
}
public Bee anotherDay(){
return this;
}
public boolean eat(){
if(Hive.honey > 2){
hive.takeHoney(2);
return true;
}else{
return false;
}
}
}
这绝对没有任何作用:
public Queen(Hive hive){
}
您将它分配到类中的某个字段的什么位置?回答你没有
解决方案:你应该
public Queen(Hive hive){
this.hive = hive;
}
当然,还要给女王一块叫做蜂巢的场地。然后你就可以使用它了。这完全没有任何作用:
public Queen(Hive hive){
}
您将它分配到类中的某个字段的什么位置?回答你没有
解决方案:你应该
public Queen(Hive hive){
this.hive = hive;
}
当然,还要给女王一块叫做蜂巢的场地。然后您就可以使用它了。您必须创建一个配置单元实例来从配置单元调用方法
public class Queen extends Bee{
int type = 1;
--> Hive hive = null;
public Queen(Hive h){
--> hive = h;
}
public Queen(int type, int age, int health){
super(type, age, health);
}
public Bee anotherDay(){
return this;
}
public boolean eat(){
--> if(hive.honey > 2){
hive.takeHoney(2);
return true;
}else{
return false;
}
}
}
必须创建配置单元实例才能从配置单元调用方法
public class Queen extends Bee{
int type = 1;
--> Hive hive = null;
public Queen(Hive h){
--> hive = h;
}
public Queen(int type, int age, int health){
super(type, age, health);
}
public Bee anotherDay(){
return this;
}
public boolean eat(){
--> if(hive.honey > 2){
hive.takeHoney(2);
return true;
}else{
return false;
}
}
}
你指的是静态的蜂巢。您需要实例化它,然后调用该对象上的方法
public class Queen extends Bee{
int type = 1;
Hive h;
public Queen(Hive hive){
h = hive;
}
public Queen(int type, int age, int health){
super(type, age, health);
}
public Bee anotherDay(){
return this;
}
public boolean eat(){
if(h.honey > 2){
h.takeHoney(2);
return true;
}else{
return false;
}
}
}您在静态地引用蜂巢。您需要实例化它,然后调用该对象上的方法
public class Queen extends Bee{
int type = 1;
Hive h;
public Queen(Hive hive){
h = hive;
}
public Queen(int type, int age, int health){
super(type, age, health);
}
public Bee anotherDay(){
return this;
}
public boolean eat(){
if(h.honey > 2){
h.takeHoney(2);
return true;
}else{
return false;
}
}
}“它不起作用”……请说得更具体些。您似乎面临的问题到底是什么?您没有将配置单元存储在实例变量中。该构造函数将
hive
作为参数,但对其不做任何处理。您应该始终复制/粘贴收到的确切错误消息。“它不工作”…请更具体一些。您似乎面临的问题到底是什么?您没有将配置单元存储在实例变量中。该构造函数将hive
作为参数,但不使用它。您应该始终复制/粘贴您收到的确切错误消息。实际上,如果方法是静态的,则无需创建实例。你甚至可以做Hive-Hive;蜂巢;蜂蜜(2)代码>和它将工作。但是OP显然不理解静态变量/方法和实例变量/方法之间的区别。谢谢-这完全起作用了。请注意,其他可能看到这个问题而不是“hive=hive;”的人,正如您所说,我必须输入“this.hive=hive;”才能编译代码。再次感谢是的,抱歉我没有测试它。应该是“这个蜂巢”。。我更新了代码,但我只是将构造函数的参数更改为hActually,如果方法是静态的,那么就不需要创建实例。你甚至可以做Hive-Hive;蜂巢;蜂蜜(2)代码>和它将工作。但是OP显然不理解静态变量/方法和实例变量/方法之间的区别。谢谢-这完全起作用了。请注意,其他可能看到这个问题而不是“hive=hive;”的人,正如您所说,我必须输入“this.hive=hive;”才能编译代码。再次感谢是的,抱歉我没有测试它。应该是“这个蜂巢”。。我更新了代码,但我只是将构造函数的参数更改为HN,如果其中的所有内容都是静态的,则无需创建配置单元。如果其中的所有内容都是静态的,则无需创建配置单元。