Java中替代C# ref/out 关键字方案:
2014-05-08 23:49
253 查看
刚学习Java不久,今天遇到一个问题,需要在方法中修改传入的对象的值,确切的说是需要使用一个方法,创建一个对象,并把其引用返回,熟悉C#的我
的第一反应就是C#中的ref/out关键字,结果发现Java中没有类似的关键字,所以只能想想如何解决此问题.
参数传递:
方法的参数传递有两种,一种是值传递,一种是引用传递,但是其实都是拷贝传递。
值传递:就是把传递的【数据本身拷贝一份】,传入方法中对其进行操作,拷贝的是值。
引用传递:就是把指向某个对象的【引用拷贝一份】,传入方法中通过此引用可以对对象进行操作。
那么就我当前的问题而言,我需要一个方法,我传入一个引用,在方法中创建所需对象.
那么在Java中就会只能给当前对象在封装一层(定义一个新类),使得需要创建的对象成为新类的成员,在方法中为成员赋值
Example:
C#:
.csharpcode,.csharpcodepre
{
font-size:small;
color:black;
font-family:consolas,"CourierNew",courier,monospace;
background-color:#ffffff;
/*white-space:pre;*/
}
.csharpcodepre{margin:0em;}
.csharpcode.rem{color:#008000;}
.csharpcode.kwrd{color:#0000ff;}
.csharpcode.str{color:#006080;}
.csharpcode.op{color:#0000c0;}
.csharpcode.preproc{color:#cc6633;}
.csharpcode.asp{background-color:#ffff00;}
.csharpcode.html{color:#800000;}
.csharpcode.attr{color:#ff0000;}
.csharpcode.alt
{
background-color:#f4f4f4;
width:100%;
margin:0em;
}
.csharpcode.lnum{color:#606060;}
.csharpcode,.csharpcodepre
{
font-size:small;
color:black;
font-family:consolas,"CourierNew",courier,monospace;
background-color:#ffffff;
/*white-space:pre;*/
}
.csharpcodepre{margin:0em;}
.csharpcode.rem{color:#008000;}
.csharpcode.kwrd{color:#0000ff;}
.csharpcode.str{color:#006080;}
.csharpcode.op{color:#0000c0;}
.csharpcode.preproc{color:#cc6633;}
.csharpcode.asp{background-color:#ffff00;}
.csharpcode.html{color:#800000;}
.csharpcode.attr{color:#ff0000;}
.csharpcode.alt
{
background-color:#f4f4f4;
width:100%;
margin:0em;
}
.csharpcode.lnum{color:#606060;}
.csharpcode,.csharpcodepre
{
font-size:small;
color:black;
font-family:consolas,"CourierNew",courier,monospace;
background-color:#ffffff;
/*white-space:pre;*/
}
.csharpcodepre{margin:0em;}
.csharpcode.rem{color:#008000;}
.csharpcode.kwrd{color:#0000ff;}
.csharpcode.str{color:#006080;}
.csharpcode.op{color:#0000c0;}
.csharpcode.preproc{color:#cc6633;}
.csharpcode.asp{background-color:#ffff00;}
.csharpcode.html{color:#800000;}
.csharpcode.attr{color:#ff0000;}
.csharpcode.alt
{
background-color:#f4f4f4;
width:100%;
margin:0em;
}
.csharpcode.lnum{color:#606060;}
Java:
.csharpcode,.csharpcodepre
{
font-size:small;
color:black;
font-family:consolas,"CourierNew",courier,monospace;
background-color:#ffffff;
/*white-space:pre;*/
}
.csharpcodepre{margin:0em;}
.csharpcode.rem{color:#008000;}
.csharpcode.kwrd{color:#0000ff;}
.csharpcode.str{color:#006080;}
.csharpcode.op{color:#0000c0;}
.csharpcode.preproc{color:#cc6633;}
.csharpcode.asp{background-color:#ffff00;}
.csharpcode.html{color:#800000;}
.csharpcode.attr{color:#ff0000;}
.csharpcode.alt
{
background-color:#f4f4f4;
width:100%;
margin:0em;
}
.csharpcode.lnum{color:#606060;}
.csharpcode,.csharpcodepre
{
font-size:small;
color:black;
font-family:consolas,"CourierNew",courier,monospace;
background-color:#ffffff;
/*white-space:pre;*/
}
.csharpcodepre{margin:0em;}
.csharpcode.rem{color:#008000;}
.csharpcode.kwrd{color:#0000ff;}
.csharpcode.str{color:#006080;}
.csharpcode.op{color:#0000c0;}
.csharpcode.preproc{color:#cc6633;}
.csharpcode.asp{background-color:#ffff00;}
.csharpcode.html{color:#800000;}
.csharpcode.attr{color:#ff0000;}
.csharpcode.alt
{
background-color:#f4f4f4;
width:100%;
margin:0em;
}
.csharpcode.lnum{color:#606060;}
.csharpcode,.csharpcodepre
{
font-size:small;
color:black;
font-family:consolas,"CourierNew",courier,monospace;
background-color:#ffffff;
/*white-space:pre;*/
}
.csharpcodepre{margin:0em;}
.csharpcode.rem{color:#008000;}
.csharpcode.kwrd{color:#0000ff;}
.csharpcode.str{color:#006080;}
.csharpcode.op{color:#0000c0;}
.csharpcode.preproc{color:#cc6633;}
.csharpcode.asp{background-color:#ffff00;}
.csharpcode.html{color:#800000;}
.csharpcode.attr{color:#ff0000;}
.csharpcode.alt
{
background-color:#f4f4f4;
width:100%;
margin:0em;
}
.csharpcode.lnum{color:#606060;}
ref/out关键字:http://msdn.microsoft.com/zh-cn/library/14akc2c7.aspx
的第一反应就是C#中的ref/out关键字,结果发现Java中没有类似的关键字,所以只能想想如何解决此问题.
参数传递:
方法的参数传递有两种,一种是值传递,一种是引用传递,但是其实都是拷贝传递。
值传递:就是把传递的【数据本身拷贝一份】,传入方法中对其进行操作,拷贝的是值。
引用传递:就是把指向某个对象的【引用拷贝一份】,传入方法中通过此引用可以对对象进行操作。
那么就我当前的问题而言,我需要一个方法,我传入一个引用,在方法中创建所需对象.
那么在Java中就会只能给当前对象在封装一层(定义一个新类),使得需要创建的对象成为新类的成员,在方法中为成员赋值
Example:
C#:
///<summary>
///model
///</summary>
publicclassStudent
{
publicstringName;
publicintAge;
}
///方法:
///<summary>
///错误示范
///
///student这个引用是拷贝传入的,创建新的对应Student
///把引用赋给student,当方法退栈之后student引用回到压栈前
///则Student对象引用数为0,等待GC,引用student依旧是原来的值(逻辑地址)
///</summary>
///<paramname="student"></param>
staticvoidcreateStudent(Studentstudent)
{
student=newStudent{Name="StephenLee",Age=1};
}
///<summary>
///正确做法
///
///通过ref关键字,把操作权让给方法内部
///</summary>
///<paramname="student"></param>
staticvoidcreateStudent(refStudentstudent)
{
student=newStudent{Name="StephenLee",Age=1};
}
.csharpcode,.csharpcodepre
{
font-size:small;
color:black;
font-family:consolas,"CourierNew",courier,monospace;
background-color:#ffffff;
/*white-space:pre;*/
}
.csharpcodepre{margin:0em;}
.csharpcode.rem{color:#008000;}
.csharpcode.kwrd{color:#0000ff;}
.csharpcode.str{color:#006080;}
.csharpcode.op{color:#0000c0;}
.csharpcode.preproc{color:#cc6633;}
.csharpcode.asp{background-color:#ffff00;}
.csharpcode.html{color:#800000;}
.csharpcode.attr{color:#ff0000;}
.csharpcode.alt
{
background-color:#f4f4f4;
width:100%;
margin:0em;
}
.csharpcode.lnum{color:#606060;}
.csharpcode,.csharpcodepre
{
font-size:small;
color:black;
font-family:consolas,"CourierNew",courier,monospace;
background-color:#ffffff;
/*white-space:pre;*/
}
.csharpcodepre{margin:0em;}
.csharpcode.rem{color:#008000;}
.csharpcode.kwrd{color:#0000ff;}
.csharpcode.str{color:#006080;}
.csharpcode.op{color:#0000c0;}
.csharpcode.preproc{color:#cc6633;}
.csharpcode.asp{background-color:#ffff00;}
.csharpcode.html{color:#800000;}
.csharpcode.attr{color:#ff0000;}
.csharpcode.alt
{
background-color:#f4f4f4;
width:100%;
margin:0em;
}
.csharpcode.lnum{color:#606060;}
//Client
staticvoidMain(string[]args)
{
Console.WriteLine("错误示范");
Studentstudent1=null;
createStudent(student1);
if(student1==null)
Console.WriteLine("创建对象失败");
else
Console.WriteLine("创建对象成功,Name:"+student1.Name);
Console.WriteLine("正确做法");
Studentstudent2=null;
createStudent(refstudent2);
if(student2==null)
Console.WriteLine("创建对象失败");
else
Console.WriteLine("创建对象成功,Name:"+student2.Name);
}
.csharpcode,.csharpcodepre
{
font-size:small;
color:black;
font-family:consolas,"CourierNew",courier,monospace;
background-color:#ffffff;
/*white-space:pre;*/
}
.csharpcodepre{margin:0em;}
.csharpcode.rem{color:#008000;}
.csharpcode.kwrd{color:#0000ff;}
.csharpcode.str{color:#006080;}
.csharpcode.op{color:#0000c0;}
.csharpcode.preproc{color:#cc6633;}
.csharpcode.asp{background-color:#ffff00;}
.csharpcode.html{color:#800000;}
.csharpcode.attr{color:#ff0000;}
.csharpcode.alt
{
background-color:#f4f4f4;
width:100%;
margin:0em;
}
.csharpcode.lnum{color:#606060;}
Java:
/**Model
*@authorStephen
*
*/
publicclassStudent
{
publicStringName;
publicintAge;
publicStudent(Stringname,intage)
{
this.Name=name;
this.Age=age;
}
}
.csharpcode,.csharpcodepre
{
font-size:small;
color:black;
font-family:consolas,"CourierNew",courier,monospace;
background-color:#ffffff;
/*white-space:pre;*/
}
.csharpcodepre{margin:0em;}
.csharpcode.rem{color:#008000;}
.csharpcode.kwrd{color:#0000ff;}
.csharpcode.str{color:#006080;}
.csharpcode.op{color:#0000c0;}
.csharpcode.preproc{color:#cc6633;}
.csharpcode.asp{background-color:#ffff00;}
.csharpcode.html{color:#800000;}
.csharpcode.attr{color:#ff0000;}
.csharpcode.alt
{
background-color:#f4f4f4;
width:100%;
margin:0em;
}
.csharpcode.lnum{color:#606060;}
/**错误示范,原因同C#
*@paramstudent
*/
privatevoidcreateStudent(Studentstudent)
{
student=newStudent("StephenLee",1);
}
/**正确做法
*@paramstudentPack
*/
privatevoidcreateStudent(StudentPackstudentPack)
{
studentPack.student=newStudent("StephenLee",1);
}
/**包装器
*@authorStephen
*
*/
publicclassStudentPack
{
publicStudentstudent;
}
.csharpcode,.csharpcodepre
{
font-size:small;
color:black;
font-family:consolas,"CourierNew",courier,monospace;
background-color:#ffffff;
/*white-space:pre;*/
}
.csharpcodepre{margin:0em;}
.csharpcode.rem{color:#008000;}
.csharpcode.kwrd{color:#0000ff;}
.csharpcode.str{color:#006080;}
.csharpcode.op{color:#0000c0;}
.csharpcode.preproc{color:#cc6633;}
.csharpcode.asp{background-color:#ffff00;}
.csharpcode.html{color:#800000;}
.csharpcode.attr{color:#ff0000;}
.csharpcode.alt
{
background-color:#f4f4f4;
width:100%;
margin:0em;
}
.csharpcode.lnum{color:#606060;}
//Client
StudentPackstudentPack=newStudentPack();
createStudent(studentPack);
System.out.println(studentPack.student.Name);
.csharpcode,.csharpcodepre
{
font-size:small;
color:black;
font-family:consolas,"CourierNew",courier,monospace;
background-color:#ffffff;
/*white-space:pre;*/
}
.csharpcodepre{margin:0em;}
.csharpcode.rem{color:#008000;}
.csharpcode.kwrd{color:#0000ff;}
.csharpcode.str{color:#006080;}
.csharpcode.op{color:#0000c0;}
.csharpcode.preproc{color:#cc6633;}
.csharpcode.asp{background-color:#ffff00;}
.csharpcode.html{color:#800000;}
.csharpcode.attr{color:#ff0000;}
.csharpcode.alt
{
background-color:#f4f4f4;
width:100%;
margin:0em;
}
.csharpcode.lnum{color:#606060;}
ref/out关键字:http://msdn.microsoft.com/zh-cn/library/14akc2c7.aspx
相关文章推荐
- C# ref与out关键字
- C# 参考之方法参数关键字:params、ref及out
- c#中ref和out 关键字
- C# 参考之方法参数关键字:params、ref及out
- C#中ref和out关键字的区别?
- DEMO[C#]refKeyword and outKeyword 关于ref与out关键字区别的DEMO
- c#中ref和out 关键字
- c#关键字,ref和out
- C#中关键字ref与out的区别(转)
- C# params, ref, out 关键字
- C#中的ref out param关键字
- C#方法的参数关键字:ref、out、params
- 在c#中ref 和 out关键字的联系和区别
- C# 参考之方法参数关键字:params、ref及out
- DEMO[C#]refKeyword and outKeyword 关于ref与out关键字区别的DEMO
- c#中ref和out 关键字
- C# 方法参数关键字 params、ref及out
- C#方法参数传递-同时使用ref和out关键字
- c#中的关键字out是什么意思?ref是什么意思?它们有什么区别?
- C# 参考之方法参数关键字:params、ref及out 引用之csdn的好友 小常