** | ** |
---|---|
常量 | 与类关联的常量值 |
字段 | 类的变量 |
方法 | 类可执行的计算和操作 |
属性 | 与读写类的命名属性相关联的操作 |
索引器 | 与以数组方式索引类的实例相关联的操作 |
事件 | 可由类生成的通知 |
运算符 | 类所支持的转换和表达式运算符 |
构造函数 | 初始化类的实例或类本身所需的操作 |
析构函数 | 在永久丢弃类的实例之前执行的操作 |
类型 | 类所声明的嵌套类型 |
总结:整个都是字段,属性是字段的包装器,索引器是数组类型字段的包装器,常量是定值的字段、
字段 field
字段 (field) 是一种表示与对象或类关联的变量的成员。field-declaration 用于引入一个或多个给定类型的字段。
字段命名规则为:名词+首字母大写
字段和静态字段
class Program
{
static void Main(string[] args)
{
List<Student> stuList = new List<Student>();
for (int i = 0; i < 100; i++)
{
Student stu = new Student();
stu.Score = 100-i;
stu.Name = "wangming";
stu.Age = 22;
stuList.Add(stu);
}
double totalScore = 0;
int totalAge = 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.ReportAverageAge();
Student.ReportAverageScore();
}
}
class Student {
public int Age;
public string Name;
public double Score;
public static int Amount;
public static double AverageAge;
public static double AverageScore;
public Student()
{
Amount++;
}
public static void ReportAmount()
{
Console.WriteLine(Amount);
}
public static void ReportAverageAge()
{
Console.WriteLine(Student.AverageAge);
}
public static void ReportAverageScore()
{
Console.WriteLine(Student.AverageScore);
}
静态构造器先被调用且仅调用一次,实例构造器后调用且每次声明新实例都将调用。
readonly
实例只读字段
- 声明时赋值
静态只读字段
- 静态构造函数中设置值
声明时设置值
class Program { static void Main(string[] args) { Console.WriteLine(Brush.DefultColor.Red); Console.WriteLine(Brush.DefultColor.Blue); Console.WriteLine(Brush.DefultColor.Green); } } struct Color { public int Red; public int Green; public int Blue; } class Brush { public static readonly Color DefultColor; // public static readonly Color DefultColor = new Color() { Red = 0, Green = 0, Blue = 0 }; static Brush() { DefultColor = new Color() { Red = 0, Green = 0, Blue = 0 }; }
结构体 struct
用户定义的数据类型
赋值和访问
class Program { static void Main(string[] args) { Student stu1=new Student(); stu1.Age = 20; stu1.Id= 1; stu1.Name = "Ma"; Student stu2 = new Student(); stu2 = stu1; //赋值 stu2.Id = 2; stu2.Name = "Zhao"; Console.WriteLine("学号{0},姓名{1},年龄{2}",stu2.Id,stu2.Name,stu2.Age); //访问 } } struct Student { public int Id; public string Name; public int Age; }
属性 property
属性 (property) 是一种用于访问对象或类的特征的成员
本质是字段,通过方法来实现其功能
属性与方法:
public int Age
{
get { return age = 20; }
set {
if (value < 0 || value > 120)
{
throw new Exception("Age has an error!");
}
else
{
this.age = value;
}
}
}
public void SetAge(int value)
{
if (value < 0 || value > 120)
{
throw new Exception("Age has an error!");
}
else
{
this.age = value;
}
}
public int GetAge()
{
return this.age;
}
完整声明和简略声明
propfull
prop
编辑-重构-封装字段
只读属性与内部写属性
动态计算属性
非经常使用canwork属性
如果经常判断canwork属性用下面的
public int Age
{
get { return age = 20; }
set
{
if (value < 0 || value > 120)
{
throw new Exception("Age has an error!");
}
else
{
this.age = value;
this.CaculateCanWork();
}
}
}
private bool canWork;
public bool CanWork
{
get {
return this.canWork; }
}
public void CaculateCanWork()
{
if (this.age >= 16)
{
this.canWork = true;
}
else this.canWork = false;
}
索引器indexer
使对象能够用与数组相同的方式进行索引.
- 一般为集合类型,也有非集合类型
- 对于字段为数组类型,用索引来防止污染
- 访问形式为 实例名[实参]
- 必须为实例的
this 用法:
- 实例构造函数中使用
- 将当前对象作为参数传递到其他方法中
- 声明索引器
class Employee
{
private string name;
private decimal salary;
public Employee(string name,decimal salary)
{
this.name = name;
this.salary = salary; //用法一:此salary非彼salary
}
public decimal Salary
{
get { return this.salary; }
}
public void PrintEmployee()
{
Console.WriteLine("Name:{0}", this.name);
Console.WriteLine("Tax:{0}", Tax.CaclulateTax(this));//用法二:对象做参数
}
}
class Tax
{
public static decimal CaclulateTax(Employee em)
{
if (em.Salary <= 5000)
{
return 0;
}
else
{
return (em.Salary - 5000) * 0.25m;
}
}
}
索引器集合实现
Student stu = new Student();
Console.WriteLine(stu[0]);
stu[0] = 10;
stu[1] = 20;
Console.WriteLine(stu[0]);
Console.WriteLine(stu[1]);
private int[] score = new int[3];
public int? this[int n]
{
get {
return this.score[n];
/* return the specified index here */ }
set {
if (value.HasValue==false)
{
throw new Exception("值不能为空!");
}
if (value.Value>100)
{
this.score[n] = 100;
}
else if(value.Value<0)
{
this.score[n] = 0;
}
else
{
this.score[n] = value.Value;
}
/* set the specified index to value here */ }
}
索引器的非集合实现
class Program
{
static void Main(string[] args)
{
Student stu1 = new Student();
stu1["math"] = 90;
stu1["math"] = 100;
stu1["English"] = 89;
var mathScore = stu1["math"];
var engScore = stu1["English"];
Console.WriteLine(mathScore);
Console.WriteLine(engScore);
}
}
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 -1;
}
/* return the specified index here */ }
set {
if (value.HasValue==false)
{
throw new Exception("值不能为空!");
}
if (this.scoreDictionary.ContainsKey(subject))
{
this.scoreDictionary[subject] = value.Value;
}
else
{
this.scoreDictionary.Add(subject, value.Value);
}
/* set the specified index to value here */ }
}
}
常量 const
- 隶属于类型
- 编译时直接用值代替
class Program
{
static void Main(string[] args)
{
Console.WriteLine(Student.sex);
Console.WriteLine(Student.website);
}
}
class Student
{
public const string sex= "female";
public const string website = "http://ONE-Piece.cn";
}