一、实例字段和静态字段的功能
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;
namespace ConsoleApplication10
{
class Program
{
static void Main(string[] args)
{
Student stu1 = new Student();
stu1.Age = 40;
stu1.Score = 90;
Student stu2 = new Student();
stu1.Age = 440;
stu2.Score = 933;
List<Student> stuList = new List<Student>();
for (int i = 0; i < 100; i++)
{
Student stu = new Student();
stu.Age = 24;
stu.Score = i;
stuList.Add(stu);
}
Console.WriteLine(Student.Amount);
int totalAge = 0;
int totalScore = 0;
foreach(var stu in stuList)
{
totalAge += stu.Age;
totalScore += stu.Score;
}
Student.AverageAge = totalAge / Student.Amount;
Student.AverageScore = totalScore / Student.Amount;
Student.ReportAmount();
Student.ReportAverageScore();
}
}
class Student
{
public int Age;
public int Score;
public static int AverageAge;
public static int AverageScore;
public static int Amount;
public Student()
{
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);
}
}
}
这里public static加了这个后表示只能在类中使用而不再实例中使用
如果没有static就表示可以在实例中表示(静态不需要new一个实例)
初始化
对于实例字段而言,初始化的时间在创建的时候
对于静态字段而言,初始化的时机在运行环境加载这个数据类型的时候、
静态构造器只执行一次,如static Student(){}
readonly
只读字段,不能修改,设定了值(初始化)就不能改动了
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;
namespace ConsoleApplication10
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(Brush.DefaultColor.Red);
Console.WriteLine(Brush.DefaultColor.Green );
Console.WriteLine(Brush.DefaultColor.Blue);
}
}
struct Color
{
public int Red;
public int Green;
public int Blue;
}
class Brush
{
public static readonly Color DefaultColor = new Color() { Red = 0, Green = 0, Blue = 0 };
}
}
二、属性
get/set 方法
求三个学生的年龄值
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;
namespace ConsoleApplication10
{
class Program
{
static void Main(string[] args)
{
Student stu1 = new Student();
stu1.Age = 20.9;
Student stu2 = new Student();
stu2.Age = 30.5;
Student stu3 = new Student();
stu3.Age = 49.5;
double avgAge = (stu1.Age + stu2.Age + stu3.Age) / 3;
Console.WriteLine(avgAge);
}
}
class Student
{
private double age;
public double Age
{
get
{
return this.age;
}
set
{
if (value >= 0 && value <= 120)
{
this.age = value;//注意这里是age而不是Age,不然会造成栈溢出
}
else
{
throw new Exception("error!");
}
}
}
}
}
这里有set和get属性,就是一个变量的设置值和调用值时候,可以进行的操作,可以看到,当设置的年龄大于120岁或小于0时候,就在set中设计个报错信息,这样便不用另外写一个方法来判断这个变量是否符合数据的要求了哦
属性的声明
完整声明:打字propfull,然后按两下tab,就出现了
或者
private int a;
在a光标处,按下ctrl + r + e,也能初夏
简略声明:打字prop,然后按两下tab,就出现了
public int Age { get; set; }
属性的声明要是名词
属性与字段的关系
一般情况下,它们都用于表示实体(对象或类型)的状态
属性大多数情况是字段的包装器(wrapper)
建议:永远使用属性(而不是字段)来暴露数据,即字段永远都是privite或procted的
三、索引器
索引器(indexer)是这样一种成员:它使对象能够与数组相同的方式(即使用下标)进行索引
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;
namespace ConsoleApplication10
{
class Program
{
static void Main(string[] args)
{
Student stu = new Student();
stu["Math"] = 90;
var mathScore = stu["Math"];
Console.WriteLine(mathScore);
}
}
class Student
{
private Dictionary<string, int> scoreDictionary = new Dictionary<string, int>();
public int? this[string subject]
{
get {
if(this.scoreDictionary.ContainsKey(subject))
{
return this.scoreDictionary[subject];
}
else
{
return null;
}
}
set {
if(value.HasValue == false)
{
throw new Exception("Score cannot be null");
}
if (this.scoreDictionary.ContainsKey(subject))
{
this.scoreDictionary[subject] = value.Value;
}
else
{
this.scoreDictionary.Add(subject, value.Value);
}
}
}
}
}
最后结果为90
四、常量
常量(const)是表示常量值(即,可以在编译时计算的值)的类成员
常量隶属于类型而不是对象,即没有“实例”常量
不能够用类类型或自定义结构体类型作为常量的类型
各种“只读”的场景:
为了提高程序的可读性和执行效率——常量
为了防止对象的值被改变——只读字段
向外暴露不允许修改的数据——只读属性(静态或非静态),功能与常量有一些重叠
当希望成为常量的值器类型不能被变量声明接受时(类/自定义结构体)——静态只读字段
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;
namespace ConsoleApplication10
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(WASPEC.WebsiteURL);
}
}
class WASPEC
{
public const string WebsiteURL = "http://www.waspec.org";
}
}
程序功能:打印这个网址