2014-02-28 110 views
0

我通常在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 
+3

http://docs.oracle.com/javase/tutorial/java/javaOO/methods.html – exception1

+0

你想在'return new webPage()'中放置'new'来实现什么? –

+0

当您创建一个新对象时,您正在调用该类的构造函数,因此您需要创建一个接受参数的构造函数。 – Martin

回答

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是它的局部变量。

+0

谢谢你的回答! 但是我该如何去调用另一个Java文件中的函数呢? –

+0

请遵循本教程http://www.tutorialspoint.com/java/java_methods.htm – Sarz