您的位置:首页 > 编程语言 > Java开发

Java中替代C# ref/out 关键字方案:

2014-05-08 23:49 253 查看
刚学习Java不久,今天遇到一个问题,需要在方法中修改传入的对象的值,确切的说是需要使用一个方法,创建一个对象,并把其引用返回,熟悉C#的我
的第一反应就是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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: