刚学习Java不久,今天遇到一个问题,需要在方法中修改传入的对象的值,确切的说是需要使用一个方法,创建一个对象,并把其引用返回,熟悉C#的我
的第一反应就是C#中的ref/out关键字,结果发现Java中没有类似的关键字,所以只能想想如何解决此问题.


参数传递:
方法的参数传递有两种,一种是值传递,一种是引用传递,但是其实都是拷贝传递。

值传递:就是把传递的【数据本身拷贝一份】,传入方法中对其进行操作,拷贝的是值。
引用传递:就是把指向某个对象的【引用拷贝一份】,传入方法中通过此引用可以对对象进行操作。


那么就我当前的问题而言,我需要一个方法,我传入一个引用,在方法中创建所需对象.

那么在Java中就会只能给当前对象在封装一层(定义一个新类),使得需要创建的对象成为新类的成员,在方法中为成员赋值


Example:

C#:

    /// <summary>
    /// model
    /// </summary>
    public class Student
    {
        public string Name;
        public int Age;
    }
 
        /// 方法:
        /// <summary>
        /// 错误示范
        /// 
        /// student这个引用是拷贝传入的,创建新的对应Student 
        /// 把引用赋给student,当方法退栈之后student引用回到压栈前
        /// 则Student对象引用数为0,等待GC,引用student依旧是原来的值(逻辑地址)
        /// </summary>
        /// <param name="student"></param>
        static void createStudent(Student student)
        {
            student = new Student { Name ="Stephen Lee", Age = 1};
        }
 
        /// <summary>
        /// 正确做法
        /// 
        /// 通过ref关键字,把操作权让给方法内部
        /// </summary>
        /// <param name="student"></param>
        static void createStudent(ref Student student)
        {
            student = new Student { Name = "Stephen Lee", Age = 1 };
        }

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { 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, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { 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
        static void Main(string[] args)
        {
            Console.WriteLine("错误示范");
            Student student1 = null;
            createStudent(student1);
            if (student1 == null)
                Console.WriteLine("创建对象失败");
            else
                Console.WriteLine("创建对象成功,Name:" + student1.Name);
 
            Console.WriteLine("正确做法");
            Student student2 = null;
            createStudent(ref student2);
            if (student2 == null)
                Console.WriteLine("创建对象失败");
            else
                Console.WriteLine("创建对象成功,Name:" + student2.Name);
        }

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { 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
 * @author Stephen
 *
 */
public class Student
{
    public String Name;
    public int Age;
    
    public Student(String name,int age)
    {
        this.Name = name;
        this.Age = age;
    }
}

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { 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#
     * @param student
     */
    private void createStudent(Student student)
    {
        student = new Student("Stephen Lee",1);
    }
    
    /** 正确做法
     * @param studentPack
     */
    private void createStudent(StudentPack studentPack)
    {
        studentPack.student = new Student("Stephen Lee",1);
    }
    
    
    /** 包装器
     * @author Stephen
     *
     */
    public class StudentPack
    {
        public Student student;
    }

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { 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
        StudentPack studentPack = new StudentPack();
        createStudent(studentPack);
        System.out.println(studentPack.student.Name);
 
 

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { 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

Java中替代C# ref/out 关键字方案:的更多相关文章

  1. Java中的break和continue关键字使用总结

    java中的break和continue关键字使用总结   一.作用和区别   break的作用是跳出当前循环块(for.while.do while)或程序块(switch).在循环块中的作用是跳出 ...

  2. Java中Map遍历的四种方案

    在Java中如何遍历Map对象 方式一 这是最常见的并且在大多数情况下也是最可取的遍历方式.在键值都需要时使用. Map<Integer, Integer> map = new HashM ...

  3. 从缓存入门到并发编程三要素详解 Java中 volatile 、final 等关键字解析案例

    引入高速缓存概念 在计算机在执行程序时,以指令为单位来执行,每条指令都是在CPU中执行的,而执行指令过程中,势必涉及到数据的读取和写入. 由于程序运行过程中的临时数据是存放在主存(物理内存)当中的,这 ...

  4. Java中的static、final关键字

    static static 的含义是静态的,是一个静态修饰符,一般来说,被static修饰的有以下几种,类.变量.方法.代码块. static修饰类 Java中普通的类是不允许被声明为静态的,但是有一 ...

  5. Java中的Serializable接口transient关键字,及字节、字符、对象IO

    1.什么是序列化和反序列化Serialization是一种将对象转为为字节流的过程:deserialization是将字节流恢复为对象的过程. 2.什么情况下需要序列化a)当你想把的内存中的对象保存到 ...

  6. 理解Java中的final和static关键字

    回顾这两个关键字前,先考虑一个问题: Static变量存储在JVM中的位置,或者说static变量是如何被加载的? JVM会把类的静态方法和静态变量在类加载的过程中读入方法区(Method Area) ...

  7. 总结java中的super和this关键字

    知识点: 在java类中使用super引用父类的成分,用this引用当前对象 this可以修饰属性.构造器.方法 super可以修饰属性.构造器.方法 关于子类实例化过程中的内存分配,在下一篇博客中说 ...

  8. java中 static,final,transient,volatile关键字的作用

    static 和final static  静态修饰关键字,可以修饰 变量,程序块,类的方法: 当你定义一个static的变量的时候jvm会将将其分配在内存堆上,所有程序对它的引用都会指向这一个地址而 ...

  9. java中的权限修饰符&关键字

    1.类的权限修饰符default(不写权限修饰符),public 说明:类的权限修饰符只有default(不写权限修饰符)和public.   package world default Y N pu ...

随机推荐

  1. linux网络体系架构

    原创kylin_zeng:http://blog.csdn.net/kylin_fire_zeng  本文参考国嵌视频教程,再此感谢国嵌教育. 一.协议栈层次对比: 1)网络接口层把数据链路层和物理层 ...

  2. 利用Ihttpmodel实现网站缓存,解决Server.Transfer 直接输出HTML源代码的问题

    今天在用.NET利用IHttpModel实现网站静态缓存的时候,不知道最后为什么用 Server.Transfer(html)的时候结果输出的是HTML的源代码. 贴上源代码 using System ...

  3. Spring mvc之提交表单

    表单内容: <body> <form action="user/login"> <table> <tr> <td>用户名 ...

  4. python之数据库操作(sqlite)

    python之数据库操作(sqlite) 不像常见的客户端/服务器结构范例,SQLite引擎不是个程序与之通信的独立进程,而是连接到程序中成为它的一个主要部分.所以主要的通信协议是在编程语言内的直接A ...

  5. POJ - 1185 炮兵阵地 (状态压缩)

    题目大意:中文题目就不多说大意了 解题思路: 1.每行最多仅仅有十个位置,且不是山地就是平原,那么就能够用1表示山地,0表示平原,将每一行的状态进行压缩了 2.接着找出每行能放炮兵的状态.先不考虑其它 ...

  6. KMP算法 KMP模式匹配 一(串)

    A - KMP模式匹配 一(串) Crawling in process... Crawling failed Time Limit:1000MS     Memory Limit:131072KB  ...

  7. 实现文件下载的java代码

    实现文件下载的java代码 //这是实现下载类(servlet),详细思路代码例如以下://也可连接数据库package com.message; import javax.servlet.*;imp ...

  8. Mac os 进行Android开发笔记(2)

    Mac OS X 下开发 Android  1> MAC OS Android环境搭建: 2>MAC OS  配置 Andoid ADB: 3>MAC OS 上进行Android真机 ...

  9. Android 界面滑动实现---Scroller类 从源码和开发文档中学习(让你的布局动起来)

    在android学习中,动作交互是软件中重要的一部分,其中的Scroller就是提供了拖动效果的类,在网上,比如说一些Launcher实现滑屏都可以通过这个类去实现..   例子相关博文:Androi ...

  10. POJ 3481 &amp; HDU 1908 Double Queue (map运用)

    题目链接: PKU:http://poj.org/problem?id=3481 HDU:http://acm.hdu.edu.cn/showproblem.php?pid=1908 Descript ...