const和readonly修饰的成员,静态构造函数以及对于变量的访问{get;set}

第一,const修饰的数据类型

定义:按照public const int test=20;的格式进行声明,const修饰的数据类型叫做常量。

注意:1 访问时只能通过类名加变量名访问。

           2 必须在声明的时候就赋值。

           3 常量是不可修改的值。

代码如下:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Test1
{
    public const int test = 10;
}
public class Test : MonoBehaviour
{
    int a = Test1.test;//创建一个变量来接收这个常量
    
    private void Start()
    {
        Debug.Log("这个常量的值是"+a);
    }
}

我们不需要进行类的实例化就可以取得这个值,展示效果如下:


第二,readonly修饰的数据类型。

定义:按照public readonly int test1;的格式进行声明

注意:1 只能在声明时或者构造函数里面进行赋值。

           2 需要通过类的实例化来访问变量,但是不能修改它的值。

代码如下;

using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;

public class Test1
{
    public const int test = 10;
    public readonly int test1;
    public Test1()
    {
        test1 = 20;
    }
        
}
public class Test : MonoBehaviour
{
    int a = Test1.test;//创建一个变量来接收这个常量
    

    private void Start()
    {        
        Test1 testclass = new Test1();
        int b = testclass.test1;
        Debug.Log("这个常量的值是" + a);
        Debug.Log("test1的值是" + b);
    }
}

得到的结果:

  

第三,对于变量的访问{get;set}

定义:public int a{get;set;},这样进行访问和修改。

并且在get和set的前面可以添加访问修饰符,那么我们就可以控制外部能否访问或者修改。

举个例子,讲解外部可以访问但是不能修改。

代码如下:

using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using Unity.VisualScripting;
using UnityEngine;

public class Test1
{
   public int a { get; private set; }
   public int b { get; set; }
   public Test1 ()
    {
        a = 10;
        b = 10;
    }
}
public class Test : MonoBehaviour
{
    public Test1 test = new Test1();
    private void Start()
    {
        
        test.b = 100;
        Debug.Log("a的值为"+test.a);
        Debug.Log("b的值为" + test.b);
    }
}

结果如下:

如果进行a的修改的话,程序会报错:

如果没有加访问修饰符,就默认和前面的修饰变量的访问修饰符一样。

当然,get和set也可以写函数上去,那么我们也许就可以或者某个不可访问的值。

代码如下:

using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using Unity.VisualScripting;
using UnityEngine;

public class Test1
{
    private  int a = 10;
   public int b
    {
        get
        {
            return a;
        }
        set
        {
            a = value;//value就是输入的值。
        }
    }
   public Test1 ()
    {
        
        b = 10;
    }
}
public class Test : MonoBehaviour
{
    public Test1 test = new Test1();
    private void Start()
    {
        Debug.Log("访问b的值为" + test.b);
        test.b = 100;//value就等于10,这个时候就相当于给a赋值
        Debug.Log("修改后b的值为" + test.b);
    }
}

结果如下;


第四,静态构造函数 

定义:按照static 类名的方式进行构造函数的声明。

注意: 1 不能再静态构造函数前面添加访问修饰符。

            2 无论被实例化多少次,静态构造函数只会被调用一次,并且是被系统自动调用一次。

下面是代码:

using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using Unity.VisualScripting;
using UnityEngine;

public class Test1
{
    public Test1 ()
    {
        Debug.Log("Test1的构造函数执行");
    }
}
public class Test2
{
    static  Test2()
    {
        Debug.Log("Test2的构造函数执行");
    }
}
public class Test : MonoBehaviour
{
    private void Start()
    {
       Test1 test1 = new Test1();
       Test1 test2 = new Test1();
       Test2 test3 = new Test2();
       Test2 test4 = new Test2();//这里为了比较,对于两个类都实例化了两次
    }
    
}

下面是结果:

这就说明了静态构造函数只能被调用一次。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值