2013-09-25 137 views
0

我想做的功能可以返回几个原始数据类型来调用。原始数据类型的参考

我知道我可以返回第一个原语的函数结果,但如何返回函数参数中的原语?

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

只有这样才能将原始图像包装到Object中,如Integer或Boolean?

+0

_return几个原始数据types_你的意思是你想拥有一个具有许多原始的对象键入字段? –

+0

你想返回参数吗? – arynaq

+1

您可以添加一些示例输入和输出函数吗? – fvrghl

回答

1

只有这样才能将原始图像包装到像Integer或Boolean这样的对象中吗?

根本不是,

我认为它不是好的做法来转换变量对象并获取回用铸造或instanceof之后。

  • 您可以使用接口作为回调

例如:

OtherClass

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

它的好处,但他可以使用其他方法:)像写入文件和读取它,写入队列/队列,直接到内存:),顺便说一句,你的变种更好,更容易,但问题是如此......,他希望在.NET中使用像ref这样的输入参数,哦:((( –