日期:2014-05-18  浏览次数:21285 次

网络编程高手请进帮我读一段代码
class   User
        {
                public   readonly   TcpClient   client;
                public   readonly   StreamReader   sr;
                public   readonly   StreamWriter   sw;
                public   string   userName;
                public   User(TcpClient   client)
                {
                        this.client   =   client;
                        this.userName   =   " ";
                        NetworkStream   netStream   =   client.GetStream();
                        sr   =   new   StreamReader(netStream,   System.Text.Encoding.UTF8);
                        sw   =   new   StreamWriter(netStream,   System.Text.Encoding.UTF8);
                }
        }

代码都贴在这里了,我的理解是累类的构造函数重载对这个类的变量初始化,但是不清楚this.client   =   client;这一句应该怎么理解。请各位高手指点一下啊,谢谢!

------解决方案--------------------

this.client是这个类中的定义的public readonly TcpClient client; , 而client,是构造函数传进来的参数,很基础哦..
------解决方案--------------------
楼上的对,如果不用this.client,直接用client,那么传递进来的参数client将会把字段client给覆盖,而用this来引用字段client,并用参数client来初始化字段client
------解决方案--------------------
带参数的构造函数中

使用this关键字区分成员字段和同名的参数

----------------------


------解决方案--------------------
把参数的client赋值给public readonly TcpClient client;
用来初始化User对象的client啊