c#中设置属性 索引 接口 泛型 抽象类 委托 事件
来源:互联网 发布:荆鹏软件 编辑:程序博客网 时间:2024/05/19 11:20
类中的属性
给私有变量增加属性public class Person{private string name="";public string Name{get {return name;}set {name=value;}}}//调用Person tp =new Person();tp.Name="zxl";this.textbox1.text=tp.Name;public class person{private string name="";public string Name{get {return name;}set {if(value.length >= 2)name=value;elsename="zxl";}}}
c#中索引
public class Book{//成员变量public string temp="";//成员索引public string this [int index]{get { return temp; }set { temp= value; }}}Book tb = new Book();tb [0]="helloworld";this.textbox1.text=tb[0];
扩展改写:
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;namespace WindowsFormsApplication1{ public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { telephoneList theTelephoneList = new telephoneList(); theTelephoneList.initialize(); this.textBox1.Text = theTelephoneList["张1"].ToString(); //this.textBox1.Text = theTelephoneList[12000]; } public class Student { public string name; public int ID; } public class telephoneList { private Student[] Students; public void initialize() { Students = new Student[3]; Students[0] = new Student(); Students[0].name = "张1"; Students[0].ID = 11000; Students[1] = new Student(); Students[1].name = "张2"; Students[1].ID = 12000; Students[2] = new Student(); Students[2].name = "张3"; Students[2].ID = 13000; } public string this[int theID] { get { Student stu = null; if (Students.Length > 0) { for (int i = 0; i < Students.Length; i++) { if (Students[i].ID == theID) { stu = Students[i]; break; } } } return stu.name; } } public int this[string theName] { get { Student stu = null; if (Students.Length > 0) { for (int i = 0; i < Students.Length; i++) { if (Students[i].name == theName) { stu = Students[i]; break; } } } return stu.ID; } } } }}
接口:
private void Form1_Load(object sender, EventArgs e) { Iface z = new Person(); z.ShowFace(); } interface Iface { void ShowFace(); } class Person : Iface { public void ShowFace() { MessageBox.Show("zxl hello world !"); } }
泛型: 泛型类:带有参数的"类" 这里的参数是指 类型
private void Form1_Load(object sender, EventArgs e) { Test<string, int> z = new Test<string, int>("ZXL", 20); z.SetValue(); } public class Test<T, S> { private T name; private S age; public Test(T Name, S Age) { this.name = Name; this.age = Age; } public void SetValue() { MessageBox.Show("name is "+name.ToString()); MessageBox.Show("age is "+age.ToString()); } }
抽象类: 不能实例化! (不想让类被实例化 可以用抽象类)
private void Form1_Load(object sender, EventArgs e) { //Person tp = new Person(); //不能实例化 Person ts = new Student(); ts.SayHello(); ts.a(); } public abstract class Person { public abstract void SayHello(); public void a() { MessageBox.Show("hello"); } } public class Student : Person { public override void SayHello() { MessageBox.Show("override hello"); } }
委托:
委托是一种类型 但是委托应以了方法的类型 可以吧方法做为参数进行传递成为可能
声明: public delegate void GreetingDelegate(string name);
private void Form1_Load(object sender, EventArgs e) { //GreetPeople("zxl", EnglishGreeting); //传递英文的方法 //GreetPeople("张星星", ChineseGreeting); //传递中文的方法 //GreetingDelegate delegate1 = new GreetingDelegate(); 错误例子 委托实例是需要传递参数 方法 //创建委托调用chinese GreetingDelegate delegate1 = new GreetingDelegate(EnglishGreeting); delegate1 = ChineseGreeting; delegate1("张星星"); //委托连 //delegate1 += EnglishGreeting; //delegate1 += ChineseGreeting; } private void GreetPeople(string name,GreetingDelegate MakeGreeting) { //传入一个委托参数 MakeGreeting(name); } private void EnglishGreeting(string name) { MessageBox.Show("Morning,"+name); } private void ChineseGreeting(string name){ MessageBox.Show("你好," + name); }
事件:热水器事件
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using Delegate; //引入 命名空间namespace WindowsFormsApplication1{ public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { Heater heater = new Heater(); Alarm alarm = new Alarm(); Display display = new Display(); heater.BoilEvent += alarm.MakeAlert;//注册方法 heater.BoilEvent += alarm.MakeAlert;//注册方法 heater.BoilEvent += display.ShowMsg;//注册方法 heater.BoilWater(); //烧水,温度懂啊99时触发 } }}namespace Delegate { //热水器类 public class Heater { private int temperature = 0; //声明成员变量 public delegate void BoilHandler(int param); //声明委托 public event BoilHandler BoilEvent; //烧水方法 public void BoilWater() { for (int i = 1; i <= 100; i++) { temperature++; if (temperature >= 99 && BoilEvent != null) { BoilEvent(temperature); //调用注册方法 } } } } //报警器类 public class Alarm { public void MakeAlert(int param) { MessageBox.Show("水开了 温度为:"+param.ToString()); } } //显示器类 public class Display{ public void ShowMsg(int param) { MessageBox.Show("显示当前温度:"+param.ToString()); } }}
- c#中设置属性 索引 接口 泛型 抽象类 委托 事件
- C#属性-索引器-里氏替换-多态-虚方法-抽象-接口-泛型-
- 委托 事件 密封类 静态类 抽象类 接口
- 【设计模式】抽象类、接口、委托和事件
- c#中接口和类的有什么异同,c#中的委托是什么?事件是不是一种委托?事件和委托的关系
- C#线程中使用委托方式设置控件属性
- C#中关于“接口,抽象类,类”
- C#中抽象类和接口
- C#中接口和抽象类
- C#中抽象类 与 接口 理解
- c#中委托与事件
- C#中委托和事件
- C#中事件与委托
- C#中事件和委托
- C#中委托和事件
- C#中委托与事件
- C#中委托与事件
- C#中 委托与事件
- 用shell修改crontab
- 浅谈SQL Server中的三种物理连接操作
- MySQL中的全文检索(1)
- android如何静默方式来安装apk
- JAVA SPLIT的用法
- c#中设置属性 索引 接口 泛型 抽象类 委托 事件
- 使用http_parser解析URL
- linux poll function
- MySQL中的全文检索(2)
- 【供参考,博主已经没有实验环境了】【中山大学东校区】【中大】【tp-link wr703n】【openwrt】【校园网inode】【无线路由】新版教程-搭载最小的认证客户端【C版oh3c】
- Extjs407 getValue()和getRawValue() 区别
- 轻松解决ubuntu下音乐播放器Rhythmbox乱码问题 .
- 位运算
- 行式报表插入数据时复制行信息