前言:
现在开始学C#,在学长的帮助下写了个小索引,类之间关系还没弄懂,不过果断还是先发博一下,方便复习巩固。
namespace ConsoleApplication1 { class Program { static void Main(string[] args) { Xuesheng student1 = new Xuesheng(); student1.dingyi(); string name = student1[112]; Console.WriteLine("{0}", name); int phone = student1["李四"]; Console.WriteLine("{0}", phone); } } } public class Xuesheng { private student[] students; public void dingyi() { students = new student[2]; students[0] = new student(); students[0].name = "张三"; students[0].phone = 112; students[1] = new student(); students[1].name = "李四"; students[1].phone=110; } public String this[int thephone] { get { student st = new student() ; foreach (student s in students) { if (thephone==s.phone) { st = s; } } return st.name; } } public int this[String thename] { get { student st = new student(); foreach(student s in students) { if (thename == s.name) { st = s; } return st.phone; } return st.phone; } } } public class student { public string name; public int phone; }
在这里实现了一个类数组的索引功能即输入phone输出相对应的学生姓名。