2013-09-25 4 views
0

호출자에게 여러 가지 기본 데이터 형식을 반환 할 수있는 함수를 만들고 싶습니다.원시 데이터 형식 참조

첫 번째 프리미티브에 대한 함수 결과를 반환 할 수 있지만 함수 param에서 프리미티브를 반환하는 방법을 알고 있습니까?

public boolean myFunc(boolean outAnotherPrimitive) 
{ 
outAnotherPrimitive = true; //need to return value to caller 
return true; 
} 

정수 또는 부울과 같이 Object로 감싸기 위해 프리미티브를 반환하는 유일한 방법입니까?

+0

_return 몇 가지 기본 데이터 types_ 당신이 원시 많이 가진 개체를 갖고 싶어 의미합니까 입력란? –

+0

매개 변수도 반환 하시겠습니까? – arynaq

+1

함수의 샘플 입력과 출력을 추가 할 수 있습니까? – fvrghl

답변

1

정수 또는 부울과 같이 Object로 감싸기 위해 프리미티브를 반환하는 유일한 방법입니까?

전혀

,

나는이 아니 좋은 연습이 변수는 Object로 변환 후 것은 다시 캐스트 또는 instanceof과를 가져 생각합니다.

  • 인터페이스는 콜백으로 사용할 수 있습니다.

예 :

OtherClass

public class OtherClass{ 

.... 

public void myFunc(boolean anotherPrimitive, MyinterfaceItf myItf) 
{ 
boolean bool = false; 
int a = 1; 
    myItf.onFinish(bool, a) 
} 
.... 
} 

MyClass에 :

public class MyClass implements MyinterfaceItf { 

.... 

private void foo() 
{ 
    MyinterfaceItf itf = this; 

    myFunc(true, itf); 
} 

@override 
public void onFinish(bool, a){ 
    // here you can get your primitive data 
} 

} 

인터페이스

public interface MyinterfaceItf{ 
public void onFinish(bool, a); 
} 
  • 다른 옵션은 글로벌

예를 들어 변수를 사용하는 :

private boolean bool = false; 
private int num = 0; 


public boolean myFunc(boolean anotherPrimitive) 
{ 
bool = anotherPrimitive; 
num = 10; 
//.... 
} 
  • 다음 옵션을 새로운 클래스를 생성하고 대신 프리미티브를 사용 할 수 있습니다.

예 :

public class NewClass{ 
private boolean bool = false; 
private int num = 0; 
//... 

public void setBool(boolean flag){ 
    this.bool = flag; 
    } 
} 

public boolean myFunc(boolean anotherPrimitive, NewClass newClass) 
{ 
    return newClass.setBool(true); 
    } 

는 (나는 지역 편집기에서 쓴 구문 죄송합니다)

+0

좋은,하지만 그는 다른 접근법을 사용할 수 있습니다 :) 파일에 쓰기 및 읽고, queque/dequeue, 메모리에 직접 작성 :) BTW, 귀하의 변종 더 쉽습니다,하지만 질문은 너무 ..., 그는 .NET에서 ref와 같은 입력 매개 변수를 사용하려고합니다. 오 : ((( –