cstimothy17-字段,属性,索引器,常量
017 字段、属性、索引器、常量 · 语雀https://www.yuque.com/yuejiangliu/dotnet/timothy-csharp-017
程序=数据+算法
字段,属性,索引器,常量都是表示数据的
C#类型所具有的成员:
字段√
属性√
索引器√
常量√
方法
事件
构造函数
析构函数
运算符
类类型
字段-成员变量
实例字段-对象
static字段-类
字段初始化的时机
无显式初始化时,字段获得其类型的默认值
值类型-按位归0
引用类型-null
实例字段在对象创建时被加载
static字段在类被加载时,static字段只能被初始化1次,在类被加载时,会调用static构造器,而且static构造器只能被调用1次,所以static初始化可以使用初始化器(=xxx)显式初始化或者在static构造器中初始化
readonly只读字段-为实例/类型保存希望一旦被初始化后就不希望再改变的值
实例只读字段-只有1次机会被赋值-在构造器中,使用带参的构造器
static只读字段-只有1次机会被初始化-在static构造器中
//实例字段和static字段
//readonly字段
using System.Collections.Generic;
namespace cstimothy171
{class Program{static void Main(string[] args){List<Student> stuList = new List<Student>();for (int i = 0; i < 100; i++){Student stu = new Student(i);stu.Age = 24;stu.Score = i;stuList.Add(stu);}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.ReportAverageAge();Student.ReportAverageScore();}}class Student{public readonly int ID;public int Age=0;public int Score=0;public static int AverageAge;public static int AverageScore;public static int Amount;public Student(int id){Student.Amount++;this.ID = id;}static Student(){Student.Amount = 0;Student.AverageAge = 0;Student.AverageScore = 0;}~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);}}
}
字段由来
字段在cyy已经存在
cyy-cpp-java-c#
字段声明
特性opt+有效的修饰符组合opt+数据类型type+变量声明器;
有效的修饰符组合
public static int Amount;
public readonly int ID;
public static readonly
变量声明器:
1.变量名
2.变量名+变量的初始化器
变量名=初始值
在声明时显式初始化和在构造器中进行初始化效果相同
属性-属性是字段的自然扩展
对外-暴露数据,数据可以是存储在字段里的,也可以是动态计算出来的
对内-保护字段不被非法值污染
属性由Get/Set方法对进化而来
字段->属性:
属性由get/set方法对进化而来
//namespace cstimothy172
//{
// class Program
// {
// static void Main(string[] args)
// {
// try
// {
// Student stu1 = new Student();
// stu1.SetAge(20);
// Student stu2 = new Student();
// stu1.SetAge(20);
// Student stu3 = new Student();
// stu1.SetAge(200);
// int avgAge = (stu1.GetAge() + stu2.GetAge() + stu3.GetAge()) / 3;
// Console.WriteLine(avgAge);
// }
// catch (Exception ex)
// {
// Console.WriteLine(ex.Message);
// }
// }
// }
// class Student
// {
// private int age;
// public int GetAge()
// {
// return this.age;
// }
// public void SetAge(int value)
// {
// if (value >= 0 && value <= 120)
// {
// this.age = value;
// }
// else
// {
// throw new Exception("Age value has error");
// }
// }
// }
//}
namespace cstimothy173
{class Program{static void Main(string[] args){try{Student stu1 = new Student();stu1.Age = 20;Student stu2 = new Student();stu2.Age = 20;Student stu3 = new Student();stu3.Age = 200;int avgAge = (stu1.Age+ stu2.Age+ stu3.Age) / 3;Console.WriteLine(avgAge);}catch (Exception ex){Console.WriteLine(ex.Message);}}}class Student{//propfull//value是上下文关键字private int age;public int Age{get { return age; }set{if (value >= 0 && value <= 120){age = value;}else{throw new Exception("Age value has error");}}}}
}
属性的声明
完整声明propfull-后台变量和访问器
简略声明prop-只有访问器-不受保护,只为传递数据用
特性opt+有效的修饰符组合+type+属性名{访问器}
public static
只读属性-只有get没有set
属性有get和set,但是set为private,只能在类体内赋值
只写属性-只有set没有get-less
属性是字段的包装器
永远使用属性来暴露数据
字段永远都是private或protected的
委托是方法的包装器
//属性
//实例属性和static属性
namespace cstimothy174
{class Program{static void Main(string[] args){try{Student.Amount = -1;}catch (Exception ex){Console.WriteLine(ex.Message);}}}class Student{//实例属性private int age;public int Age{get { return age; }set{if (value >= 0 && value <= 120){age = value;}else{throw new Exception("Age value has error");}}}//static属性private static int amount;public static int Amount{get { return amount; }set{if (value >= 0){amount = value;}else{throw new Exception("Amount must greater than 0");}}}}
}
//属性的set为private-只能在类体内赋值
//不同于只读属性-没有set
namespace cstimothy175
{class Program{static void Main(string[] args){Student stu = new Student();stu.SomeMethod(1);Console.WriteLine(stu.Age);}}class Student{private int age;public int Age{get { return age; }private set { age = value; }}public void SomeMethod(int x){this.age = x;}}
}
动态计算值的属性
主动计算
被动计算
//动态计算值的属性-主动计算
//适用于CanWork使用频率低的场景,Age设定频繁的场景
namespace cstimothy176
{class Program{static void Main(string[] args){try{Student stu = new Student();stu.Age = 12;Console.WriteLine(stu.CanWork);}catch (Exception ex){Console.WriteLine(ex.Message);}}}class Student{private int age;public int Age{get { return age; }set { age = value; }}//CanWork属性并没有封装字段//它的值是实时动态计算出来的public bool CanWork{get{if (this.age >= 16){return true;}else{return false;}}}}
}
//动态计算值的属性-被动计算
//适用于canWork使用频繁的场景,每次设定Age时就计算canWork
namespace cstimothy177
{class Program{static void Main(string[] args){try{Student stu = new Student();stu.Age = 12;Console.WriteLine(stu.CanWork);}catch (Exception ex){Console.WriteLine(ex.Message);}}}class Student{private int age;public int Age{get { return age; }set{age = value;this.CalculateCanWork();}}//只读属性private bool canWork;public bool CanWork{get { return canWork; }}private void CalculateCanWork(){if (this.age >= 16){this.canWork = true;}else{this.canWork = false;}}}
}
索引器能够使对象能够能下标进行访问
一般拥有索引器的都是集合类型
此时example为讲解方便,使用非集合类型
声明索引器-indexer tab*2
//索引器
using System.Collections.Generic;
namespace cstimothy178
{class Program{static void Main(string[] args){Student stu = new Student();var mathScore = stu["Math"];Console.WriteLine(mathScore==null);stu["Math"] = 90;stu["Math"] = 100;mathScore = stu["Math"];Console.WriteLine(mathScore);}}class Student{//私有字段private Dictionary<string, int> dict = new Dictionary<string, int>();//index tab*2//此处返回值为int?-可空的int类型public int? this[string subject]{get{if (this.dict.ContainsKey(subject)){return this.dict[subject];}else{return null;}}set{//因为返回值为可空类型,所以上来先判断if (value.HasValue == false){throw new Exception("score cannot be null");}if (this.dict.ContainsKey(subject)){this.dict[subject] = value.Value;}else{this.dict.Add(subject, value.Value);}}}}
}
常量
Math.PI
public const double PI=3.14159
int.MaxValue
public const int MaxValue
int.MinValue
public const int MinValue
常量隶属于类型而不是对象,没有实例常量
类型.常量
实例常量由只读实例字段来担当-只有1次机会初始化,在声明的时候,初始化器初始化or在构造器中初始化
成员常量和局部常量
成员常量
Math.PI
public const string WebsiteURL= "xxx";
局部常量-在方法体中-const int x=100;
只读的应用场景
1.常量-在编译器的时候会用常量的值代替常量标识符
成员常量
局部常量
2.只读字段-没有实例常量,为了防止对象的某个值被修改则使用只读字段
3.只读属性-向外暴露不允许修改的数据
对象-实例只读属性
类型-static只读属性-只有get没有set-区别与set为private的属性
4.静态只读字段
常量的数据类型只能是基本数据类型
不能用类类型作为常量的类型-此时使用静态只读字段-static readonly
//常量
namespace cstimothy179
{class Program{static void Main(string[] args){Console.WriteLine(Student.WebsiteURL);}}class Student{//成员常量public const string WebsiteURL= "xxx";//静态只读字段public static readonly Building Location = new Building("some address");}class Building{public string Address { get; set; }public Building(string address){this.Address = address;}}
}
cstimothy17-字段,属性,索引器,常量相关推荐
- 【笔记】《C#高效编程改进C#代码的50个行之有效的办法》第1章C#语言习惯(1)--属性的特性以及索引器(SamWang)...
************************************************************************** 书名:<C#高效编程改进C#代码的50个行之 ...
- C#索引器的实现、索引器和属性的异同对比,这些技能你get到了嘛?
目录 什么是索引器? 如何声明索引器? 索引器和属性的异同对比 索引器实例分析 Hello!大家好,我是努力赚钱买生发水的灰小猿! 最近在用C#做开发的时候要用到索引函数,所以今天就在这里和小伙伴记录 ...
- C#语法小知识(六)属性与索引器
属性是一种成员,它提供灵活的机制来读取.写入或计算私有字段的值. 属性可用作公共数据成员,但它们实际上是称为"访问器"的特殊方法. 这使得可以轻松访问数据,还有助于提高方法的安全性 ...
- c#初学-索引器get和set的使用(泛型类)
索引器允许类或结构的实例就像数组一样进行索引.索引器类似于属性,不同之处在于它们的访问器采用参数. 在下面的示例中,定义了一个泛型类,并为其提供了简单的 get 和 set 访问器方法(作为分配和检索 ...
- C#锐利体验-第八讲 索引器与操作符重载(转)
第八讲 索引器与操作符重载 南京邮电学院 李建忠(cornyfield@263.net) 索引 C#锐利体验 "Hello,World!"程序 C#语言基础介绍 Microsoft ...
- 第 17 节 字段、属性、索引器、常量
第17节 字段.属性.索引器.常量 字段 属性(C#所特有的) 索引器(概述) 常量 类的成员 字段 什么是字段 1)字段(field)是一种表示与对象或类型(类与结构体)关联的变量(为对象或类型存储 ...
- 字段,属性,索引器,常量
这四种成员都是用来表达数据的. 1.字段(field) (1)什么是字段 字段是一种表示与对象或类型(类与结构体)关联的变量.为一个对象或类型存储数据.多个字段组合起来可以表示对象或类型的状态. 字段 ...
- java类中定义索引器,C#面向对象基础——字段、属性和索引器
关于面向对象编程,在很多语言里面都出现过,最常用的如java和c++, C#语言关于面向对象编程的规范,我觉得介于上面两者之间,我的理解是它比较偏向c++,或许是因为跟它的析构函数有关系,像java有 ...
- 【Kotlin】属性 与 幕后字段 ( 属性声明 | 属性初始化器 | 属性访问器 | field 属性幕后字段 | lateinit 延迟初始化属性 )
文章目录 I . 属性 字段 总结 II . 属性声明 III . 属性初始化器 IV . get / set 属性访问器 V . 属性幕后字段 field VI . 变量和常量的区别 VII . 延 ...
- C# 属性、索引器(二)
属性 using System; namespace runoob {public abstract class Person{public abstract string Name{get;set; ...
最新文章
- zabbix web前端取值同后端取值不一致
- android uri转drawable,Glide4(URL转File,URL转Drawable)
- 方法重写(override)注意事项和使用细节
- spock 集成测试_Spock 1.2 –轻松进行集成测试中的Spring Bean模拟
- python求同构数_用c语言求1到1000的同构数_后端开发
- java runtime ssh 后执行指令_酒后系列:被某厂面试官吊打后酒后整理的JVM干货
- 微课|Python编写代理服务器程序(48分钟)
- Windows驱动开发之DDK与WDK、WDM的区别
- 用python做股票因子分析_关于SPSS因子分析的几点总结
- linux终端命令大全(完善中)
- stata15中文乱码_如何解决 Stata 14 的中文乱码问题?Chinese support in Stata 14
- 李铁被传下课之际,梅西却要七拿金球奖了?这波预测没毛病
- Windows下查看Android手机APP日志
- 怎么在电脑端下载和编辑哔哩哔哩的视频
- 用计算机绘制函数图像ppt,ppt中怎么绘制三角函数图像?
- GMSK技术的原理(Principle of GMSK technologies)
- 纯html+css炫酷地球仪动画效果
- CSS—清除浮动的几种方式
- KJava在移动设备中的应用
- AspectJ的使用方法
热门文章
- 区块链对人工智能的变革:去中心化将带来数据新范式
- Linux(入门基础):85---Linux单一计划任务(at服务、at、atq、atrm、batch命令)
- 幼儿园案例经验迁移_【投石问路】让案例分析成为幼儿教师自我成长的阶梯
- 理解openssl协议:x509、crt、cer、key、csr、ssl、tls 这些都是什么鬼? 如何给自己网站颁发证书?
- smart3d4.4.5_在Android 5.0中使用Smart Lock,再也不必在家中解锁手机
- 【linux内核分析与应用-陈莉君】时钟中断机制
- Python拉勾网爬虫实现
- Python计算卡方值
- java控制台输出图片
- rual 1741. Communication Fiend