정수 또는 부울과 같이 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);
}
는 (나는 지역 편집기에서 쓴 구문 죄송합니다)
_return 몇 가지 기본 데이터 types_ 당신이 원시 많이 가진 개체를 갖고 싶어 의미합니까 입력란? –
매개 변수도 반환 하시겠습니까? – arynaq
함수의 샘플 입력과 출력을 추가 할 수 있습니까? – fvrghl