Java 如何在另一个方法中调用带有参数的方法?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21893371/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to call a method with parameters inside another method?
提问by user3330114
How do you call a method with parameters inside another method?
如何在另一个方法中调用带有参数的方法?
For example, I am trying to call my likes()
method inside my goFishingIn()
method but it's not compiling. Here's my code:
例如,我试图likes()
在我的goFishingIn()
方法中调用我的方法,但它没有编译。这是我的代码:
import java.util.*;
public class Fisher
{
public void keep(Fish fish)
{
if(this.numFishCaught < LIMIT)
{
fishesCaught.add(fish);
numFishCaught++;
}
}
public boolean likes(Fish fish)
{
if(fish.size >= this.keepSize && fish.species != "Sunfish")
{
return true;
}
else
{
return false;
}
}
public void goFishingIn(Pond pond)
{
pond.catchAFish();
this.likes(Fish fish);
}
}
采纳答案by dub stylee
From what it looks like, you should be able to change your goFishingIn()
method like so:
从它的外观来看,您应该能够goFishingIn()
像这样更改您的方法:
public void goFishingIn(Pond pond)
{
Fish fish = pond.catchAFish();
this.likes(fish);
}
Basically, you just need to pass an instance of fish
to the likes()
method, so you need to instantiate the fish
prior to calling the likes()
method.
基本上,您只需要将 的实例传递fish
给likes()
方法,因此您需要fish
在调用likes()
方法之前实例化。
Or, you could even shorten it up and call the catchAFish()
method from inside the parameter, if you do not need to do anything with the fish
afterwards.
或者,您甚至可以缩短它并catchAFish()
从参数内部调用该方法,如果您不需要对之后做任何事情fish
。
public void goFishingIn(Pond pond)
{
this.likes(pond.catchAFish());
}
The first method would be preferable though, as it will allow you to make any future references to the fish
object.
不过,第一种方法更可取,因为它允许您在以后对fish
对象进行任何引用。
回答by Sam I am says Reinstate Monica
if you already have a fish
somewhere, you'd call it like so
如果你已经有一个fish
地方,你会这样称呼它
this.likes(fish);
If you don't, maybe you have to make one
如果你不这样做,也许你必须做一个
Fish fish = new Fish();
this.likes(fish);
or pass one in, or do whatever to get one in scope
或者传递一个,或者做任何事情来获得一个范围
public void goFishingIn(Pond pond, Fish fish)
{
pond.catchAFish();
this.likes(fish);
}
回答by Warlord
If you are declaring a method, you state parameters with classes, such as
如果要声明方法,则使用类声明参数,例如
public void goFishingIn(Pond pond)
but when you invoke a method, you only pass a variable, there is no Class identifier.
但是当你调用一个方法时,你只传递一个变量,没有类标识符。
this.likes(Fish fish);
should be
应该
this.likes(fish);
However fish
variable is not defined anywhere inside goFishingIn()
method body, therefore you will get an error that it's undefined. You need to obtain the fish
value somewhere, or define it.
但是fish
变量没有在goFishingIn()
方法体内的任何地方定义,因此你会得到一个未定义的错误。您需要在fish
某处获取值,或定义它。
It seems to me that most reasonable in this case would be to make pond.catchAFish()
return type Fish
and then store the return value in fish
variable.
在我看来,在这种情况下最合理的是创建pond.catchAFish()
返回类型Fish
,然后将返回值存储在fish
变量中。
Fish fish = pond.catchAFish();
Then you will be able to use it.
然后你就可以使用它了。
回答by Bgvv1983
You should change
你应该改变
this.likes(Fish fish);
into
进入
this.likes(fish);
When you pass arguments to a method , you dont specify the type. You only specify the type in the method signature itself. Nog when you call this method.
当您将参数传递给方法时,您不指定类型。您只需在方法签名本身中指定类型。当你调用这个方法时 Nog。