2014-02-28 8 views
0

보통 Python으로 코딩하고 자바 학습을 시작했으나 인수로 메소드를 호출하는 것이 혼란 스럽습니다.인수를 사용하여 함수 호출하기

my_function(argument) 

이처럼 만든 후에 :

def my_function(argument): 
    #Here I can use my argument like this 
    print(argument) #if argument is a string 

을하지만 자바에서 같은 일을하는 방법을 발견하지 않았습니다 실제로 파이썬에서이 같은 인수로 함수를 호출 할 수 있습니다. 사실 나는 특히 이렇게 싶습니다

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) 당신은 void sum(double a,double b) 같은 모습이라고 지금 방법 here 그 방법의 서명에 대해 알고 있어야합니다.

참고 : 방법 void sum(double a,double b)에있는 및 b는 로컬 변수입니다.

+0

답변 해 주셔서 감사합니다. 그러나 다른 Java 파일에있는 함수를 호출하려면 어떻게해야합니까? –

+0

이 자습서를 따르십시오 http://www.tutorialspoint.com/java/java_methods.htm – Sarz