我通常用Python编写代码而且我已经开始学习Java了,但是我在调用带参数的方法时很困惑。实际上,在Python中,您可以使用如下参数调用函数:
my_function(argument)
创建它之后:
def my_function(argument):
#Here I can use my argument like this
print(argument) #if argument is a string
但我还没有找到如何在Java中做同样的事情。事实上,我特别喜欢这样做:
return new webPage() //this call my file named "webPage"
//But when I'm calling, I want to send the url of the
//web page and recover it in the other file
答案 0 :(得分:0)
是的,你可以做到,但差别不大 像:
public static void main(String[]args){
Scanner scan = new Scanner(System.in);
double a= scan.nextInt();
double b= scan.nextInt();
sum(a,b);
sub(a,b)
mul(a,b);
div(a,b);
}
void sum(double a,double b){
System.out.println("Sum :"+(a+b));
}
void sub(double a,double b){
System.out.println("Sub :"+(a-b));
}void mul(double a,double b){
System.out.println("Mul :"+(a*b));
}void div(double a,double b){
System.out.println("Div :"+(a/b));
}
控制台计算器;
在这:
像sum(a,b)
一样调用方法你应该知道方法here的签名,现在它的方法叫做void sum(double a,double b)
。
注意:方法void sum(double a,double b)
a和b是其本地变量。