黑马程序员技术交流社区

标题: 请问属性中如何点儿出方法啊???? 比如Parameters属性 [打印本页]

作者: ★心秒★    时间: 2012-4-3 17:03
标题: 请问属性中如何点儿出方法啊???? 比如Parameters属性
本帖最后由 ★心秒★ 于 2012-4-18 10:42 编辑

兄弟们,帮个忙
cmd.Parameters.Add(new SqlParameter("a", accesee));
这个怎么解释,Parameters是个属性,属性怎么可以点儿出一个方法呢???
希望可以说的具体点儿! 我看到了一篇文章如下:
///   <summary>
///   带this索引类
///   </summary>
public        class        TestThis
{
private        ArrayList        al=null;
public        TestThis(){}
public           string        this[int        index]
{
get{
if((al.Count> index)&&al!=null)
return        al[index]   as   string;
else
return        string.Empty;
}
set{}
}
public        void        Add(string        name)
{
if(al==null)
al=new   ArrayList();
al.Add(name);
}
}
///   <summary>
///   此类带一个实例化的TestThis
///   </summary>
public        class        Test
{
private        TestThis        testThis;
public        Test()
{
testThis=new   TestThis();
}
public        TestThis        TestThis
{
get        {return        testThis;}
}
}
///   <summary>
///   继承类调用例子(这个就是你所说的属性后加方法)
///   </summary>
public        class        Test1:Test
{
public        Test1()
{
this.TestThis.Add( "aaa ");
string        str1         =this.TestThis[0];

}
}





可是这个例子我还是不是很明白诶!  其中,al.count 是什么东东????  可不可以详细些讲啊???
作者: 何智杰    时间: 2012-4-3 17:10
Parameters 不是属性,是一个集合。往集合里面添加对象是用到集合的Add方法,没有问题的
al.count 是什么东东????   al也是一个集合,集合是具有Count属性,通过它可以得到集合里面元素的个数。
希望你明白
作者: ★心秒★    时间: 2012-4-3 17:26
何智杰 发表于 2012-4-3 17:10
Parameters 不是属性,是一个集合。往集合里面添加对象是用到集合的Add方法,没有问题的
al.count 是什么东 ...

有点儿高深,我还不懂什么是集合!  谢谢哈!  我知道了集合这个东西,我去学学,应该可以弄明白!!! 谢了哥们儿……
作者: 郑帅    时间: 2012-4-3 18:27
原理:
SqlCommand cmd = new SqlCommand();
            
cmd.Parameters.Add(new SqlParameter("@userName", ""));

1.使用对象cmd.Parameters时,程序在内部实例一个SqlParameterCollection对象。
2.SqlParameterCollection内部有一个Add方法。该方法接收一个SqlParameter对象。

//伪带码
public class SqlCommand
{
        private SqlParameterCollection _parameters;

        public SqlParameterCollection Parameters
        {
            get
            {
                if (this._parameters == null)
                {
                    this._parameters = new SqlParameterCollection();// 它有访问级别,一般不能直接实例出来
                }
                return this._parameters;
            }
        }

    }

    public class SqlParameterCollection
    {
        public SqlParameter Add(SqlParameter value)
        {
            this.Add(value);
            return value;
        }
    }


Parameters在SqlCommand中是一个方法。
这个方法接收的是一个SqlParameterCollection对象,这个对象中有一个Add方法.

但SqlParameterCollection不能直接实例出来,因为这个类有访问级别

提示,如果想深入了解。可以了解一个扩展方法。这里有很大的联系性。

作者: ★心秒★    时间: 2012-4-6 09:09
郑帅 发表于 2012-4-3 18:27
原理:
SqlCommand cmd = new SqlCommand();
            

谢了,郑兄!:handshake




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2