#region 常量和静态变量静态类readonly

#region 常量和静态变量静态类readonly

#region 常量和静态变量静态类readonly
        //----------------------------------------------------------------------
        //常量和静态变量,静态类
        //----------------------------------------------------------------------
        //类的静态变量和常量,都属于类而不属于对象,不能用对象来调用,只能用类名调用
        //这不同于C++,是更合理的设计
        //常量的值在类定义时就确定了,不因对象而不同,因此存放在类中更合理
        class CNormclass
        {
            class CInclass
            {
                public float fx = 20;
            }
            public int _id;
            public const string cname = "CNormalclass";

            //1,常量仅能修饰 :数字,bool,字符串,null引用
            //不能像C++那样定义一个常量对象,这真是太悲哀了,因为很多时候这可以加速数据传递,增加安全性
            //由于这个原因,C#的List.ToArray每次都只能返回一个内部数组的拷贝,因此使用list存储数量较大较复杂的数据时
            //不要轻易使用ToArray,直接用List就行了,它也支持下标索引方式取数组元素
            const CInclass lst = null;

            //2,readonly也不能实现常量对象的效果
            //readonly仅表示变量本身不能被赋值,但不阻止通过对象变量更改对象内的字段
            //onc.readonlyobj.fx = 20
            public float fx = 20;

            private readonly CInclass readonlyobj = new CInclass();
  
            public static void Test()
            {
                //1,不能调用非静态字段或方法
                //this._id = 20; //error,没有this指针

                //2,可以调用常量字段
                var lname = cname;

                var onc = new CNormclass();

                //私有变量在类的静态方法也可以访问
                //2,虽然不能更改readonlyobj本身的值,却可以更改其内部成员的值,这就是readonly的作用
                onc.readonlyobj.fx = 20; 
            }
        }
        static class C712//类中类,默认为私有
        {//静态类不能实例化,且只能声明:常量,静态常量,静态属性,静态方法
            public const int constX = 20; //1,常量
            public static int staticX = 0; //2,静态常量
            public static int ix { set; get; } //3,静态属性

            //一,【静态类中不能定义实例化字段】
            //public int _id; 

            //二,【静态类中不能定义实例化字段】
            //void Ctest(){ //【error: 静态类中不能定义实例化方法】
            //    this._id = 20;
            //}

            static void Test()//4,静态方法
            {
                //三,【静态方法中不能调用非静态变量或方法,因为没有this指针】
                //_id = 20;  //error 

                //四,【可以调用常量字段,这与C++不同】
                var c = constX;
            }

        }
        public const int ixd = 20;
        public static float fx = 20;
        public void Testff()
        {
            fx = 30; //等价于Program.fx = 30,而不是 this.fx = 30;
            Program.fx = 30;
            var tx = C712.constX;
            C712.staticX = 30;
            var ix = Program.ixd;

            //var oc7 = new C712(); //error 静态类不能创建实例
        }

 

posted on 2018-07-30 16:52 时空观察者9号 阅读(...) 评论(...) 编辑 收藏

class CubeModel : IBufferSource { private const float halfLength = 0.5f; private static readonly vec3[] positions = new vec3[] { new vec3(+halfLength, +halfLength, +halfLength), // 0 new vec3(+halfLength, +halfLength, -halfLength), // 1 new vec3(+halfLength, -halfLength, +halfLength), // 2 new vec3(+halfLength, -halfLength, -halfLength), // 3 new vec3(-halfLength, +halfLength, +halfLength), // 4 new vec3(-halfLength, +halfLength, -halfLength), // 5 new vec3(-halfLength, -halfLength, +halfLength), // 6 new vec3(-halfLength, -halfLength, -halfLength), // 7 }; private static readonly uint[] indexes = new uint[] { 0, 2, 1, 1, 2, 3, // +X faces. 0, 1, 5, 0, 5, 4, // +Y faces. 0, 4, 2, 2, 4, 6, // +Z faces. 7, 6, 4, 7, 4, 5, // -X faces. 7, 5, 3, 3, 5, 1, // -Z faces. 7, 3, 2, 7, 2, 6, // -Y faces. }; public const string strPosition = "position"; private VertexBuffer positionBuffer; // array in GPU side. private IDrawCommand drawCommand; #region IBufferSource 成员 public IEnumerable<VertexBuffer> GetVertexAttribute(string bufferName) { if (strPosition == bufferName) // requiring position buffer. { if (this.positionBuffer == null) { // transform managed array to vertex buffer. this.positionBuffer = positions.GenVertexBuffer( VBOConfig.Vec3, // mapping to 'in vec3 someVar;' in vertex shader. BufferUsage.StaticDraw); // GL_STATIC_DRAW. } yield return this.positionBuffer; } else { throw new ArgumentException("bufferName"); } } public IEnumerable<IDrawCommand> GetDrawCommand() { if (this.drawCommand == null) { // indexes in GPU side. IndexBuffer indexBuffer = indexes.GenIndexBuffer(BufferUsage.StaticDraw); this.drawCommand = new DrawElementsCmd(indexBuffer, DrawMode.Triangles); // GL_TRIANGLES. } yield return this.drawCommand; } #endregion }逐行分段解释上述程序
05-10
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值