【C#】字段

C# 类具有哪些成员?

字段

1.什么是字段

  • 字段(field)是一种表示与对象或类型(类与结构体)关联的变量
  • 字段是类型的成员,旧称“成员变量”
  • 与对象关联的字段亦称“实例字段”
  • 与类型关联的字段称为“静态字段”,由static修饰

通过一个代码来详细了解实例字段与静态字段的功能。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DateMemberExample
{
    internal class Program
    {
        static void Main(string[] args)
        {
           
            List<Student>stuList= new List<Student>(); //创建一个新的空列表
            for (int i = 0; i < 100; i++)// 循环100次
            {
                Student stu = new Student();// 创建一个新的Student对象
                stu.Age = 24;
                stu.Score = i;
                stuList.Add(stu);// 将新创建的Student对象添加到stuList中
            }
            //计算总年龄和总分数,并根据学生总数计算平均年龄和平均分数。
            int totalAge = 0;
            int totalScore = 0;
            foreach (Student stu in stuList)
            {
                totalAge += stu.Age;
                totalScore += stu.Score;
            }
            Student.AverageAge = totalAge/Student.Amount;
            Student.AverageScore = totalScore/Student.Amount;
            Student.ReportAmount();
            Student.ReportAverageAge ();
            Student.ReportAverageScore();
            Console.ReadLine();

        }
    }

    class Student
    {
        public int Age;
        public int Score;

        public static int AverageAge;
        public static int AverageScore;
        public static int Amount;

        public Student()    //每次实例被创建时,Amount加1
        {
            Student.Amount++;
        }
        public static void ReportAmount()
        {
            Console.WriteLine(Student.Amount);
        }
        public static void ReportAverageAge()
        {
            Console.WriteLine(Student.AverageAge);
        }
        public static void ReportAverageScore()
        {
            Console.WriteLine(Student.AverageScore);
        }

    }
}

代码分析:

  • 定义了两个实例字段:AgeScore
  • 定义了三个静态字段:AverageAgeAverageScoreAmount
  • 定义了一个无参数构造函数,每次创建新的Student实例时,Amount字段递增。
  • 定义了三个静态方法:ReportAmountReportAverageAgeReportAverageScore,分别用于报告学生总数、平均年龄和平均分数。


2.字段的声明

  • 尽管字段声明带有分号,但它不是语句
  • 字段的名字一定是名词

最常用的声明字段的格式是:访问级别+数据类型+变量名   或者  访问级别+ static + 数据类型+变量名。如上面的代码中:

public int Age;//最好初始化

public static int AverageAge;


3.字段的初始值

  • 无显式初始化时,字段获得其类型的默认值,所以字段“永远都不会未被初始化”
  • 实例字段初始化的时机 -- 对象创建时
  • 静态字段初始化的时机 -- 类型第一次被加载(load)时

在C#中,静态构造器主要用于初始化类的静态字段,并且在整个应用程序的生命周期中只执行一次。如上面的代码中;

static Student()    //静态构造器:对静态字段初始化,且只执行一次
{
    Student.Amount = 100;
}

//与以下的代码得到的结果一样

public Student()    //构造函数(实例构造器):每次实例被创建时,Amount加1
{
    this.Age = 0;//忽略该代码,最好在声明字段时就赋予初始值
    Student.Amount++;
}

注意:创建实例只读字段,只有在创建实例时有一次修改的机会

using System;

class Student
{
    public readonly int ID; // 声明只读字段,不能修改

    // 构造函数
    public Student(int id)
    {
        ID = id; // 在创建实例时,可以有一次机会设定ID
    }

    public void DisplayID()
    {
        Console.WriteLine("Student ID: " + ID);
    }
}

class Program
{
    static void Main()
    {
        Student stu1 = new Student(1); // 在创建实例时这样写
        stu1.DisplayID(); // 则实例stu1的ID是1

        // 下面的代码会导致编译错误,因为ID是readonly的
        // stu1.ID = 2; // 错误!
    }
}

  • 10
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值