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

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

时间:2024-10-04 16:49:15浏览次数:3  
标签:Test1 set const int System test using public 构造函数

第一,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();//这里为了比较,对于两个类都实例化了两次
    }
    
}

下面是结果:

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

标签:Test1,set,const,int,System,test,using,public,构造函数
From: https://blog.csdn.net/dailinhan123/article/details/142704079

相关文章

  • Verifying that your constructor params satisfy all assert conditions 部署测试合
     运行trufflemigrate报错***DeploymentFailed***"TestT**"hitaninvalidopcodewhiledeploying.Try:*Verifyingthatyourconstructorparamssatisfyallassertconditions.*Verifyingyourconstructorcodedoesn'taccessanarr......
  • 程序运行异常: Undefined constant"PAGE
    在使用PbootCMS时,如果遇到“Undefinedconstant'PAGE'”的错误,尤其是在后台自定义表单编辑字段时,这通常是由于PHP版本不兼容导致的问题。具体来说,某些常量或函数可能在较高版本的PHP中不再支持或行为有所变化。解决方法降低PHP版本:将PHP版本从8.0降到7.3。详细步骤检......
  • 七、Redis之sorted set
    sortedset也是Redis中常用的类型。可以用来解决热搜,排名前十等问题。ZADDZADDkey[NX|XX][GT|LT][CH][INCR]scoremember[scoremember...]zadd将多个分数和元素对添加到sortedset中。还有些选项影响了zadd的行为:XX:仅更新已存在的元素。不要添加新元素。NX:只添加......
  • 【题解】Solution Set - NOIP2024集训Day42 博弈论
    【题解】SolutionSet-NOIP2024集训Day42博弈论https://www.becoder.com.cn/contest/5574https://www.cnblogs.com/CloudWings/p/17813917.html「中山市选2009」谁能赢呢?一道经典的「二分图博弈」在棋盘问题上的应用。https://www.luogu.com.cn/article/h8a79k3i......
  • bitset
    1.位运算的常见函数__builtin_popcount(x)//x二进制内1的个数(unsignedint)__builtin_popcountll(x)//longlong版本__builtin_parity(x)//二进制下的1的个数的奇偶性__builtin_parityll(x)//longlong版本__builtin_ctz(x)//x二进制末尾0的个数__builtin_clz(x)//x二进......
  • Set<String>和JTextField
    importjavax.swing.;importjava.awt.;importjava.util.HashSet;importjava.util.Set;importjava.util.Timer;importjava.util.TimerTask;publicclassSizeyunsuan{publicstaticvoidmain(String[]args){inti=1;SetgeneratedQuestions=newHashSet&......
  • 六、redis之set
    Redis集合是成员的无序集合。可以用来保存唯一的成员。注意:对于以下的命令,涉及删除成员的,如果集合中的所有元素都被移除,则集合会被删除。如果集合原先不存在,被当作空集合。SADDSADDkeymember[member...]sadd命令将一系列成员添加到set中。SMEMBERSSMEMBERSkeysmemb......
  • 解决 DedeCMS 报错“Please set ‘request_order’”的问题
    如果你使用的是虚拟主机,无法直接修改 php.ini 文件,可以通过修改DedeCMS的代码来解决这个问题。找到 common.inc.php 文件:打开织梦CMS安装目录下的 include/common.inc.php 文件。修改代码:使用文本编辑器打开 common.inc.php 文件。找到第34行:php ......
  • 织梦错误Please set ‘request_order’
    当你在使用DedeCMS并遇到错误提示“DedeCMSError:(PHP5.3andabove)Pleaseset‘request_order’inivaluetoincludeC,GandP(recommended:‘CGP’)inphp.ini,more…”时,可以通过以下两种方法来解决这个问题:方法1:修改 php.ini 文件找到 php.ini 文件:......
  • 【VBA】RangeやCellsの範囲を移動する【Offsetを使います】
    参考元:【VBA】RangeやCellsの範囲を移動する【Offsetを使います】https://daitaideit.com/vba-range-offset/ポイントとなるVBAコードCells(1,1).Offset(2,3).Select'Cellsを2行と3列だけ移動するRange("A1").Offset(2,3).Select'Rangeをを2行と3列だけ移動するVBA......