在MyCLass类中声明10个MyStudent类的对象,如何写索引器class MyStudent
{
//构造函数
public MyStudent(string name)
{
this.name = name;
} //name字段
private string name;
//属性
public string Name
{get;set;}
}
class MyClass
{
//构造函数
public MyCLass()
{
stu[0] = new Mystudent("Liu");
stu[1] = new Mystudent("Li");
stu[2] = new Mystudent("Zhu");
}
//声明3个对象
private MyStudent[] stu = new MyStudent[3];
//索引器
public MyStudent this[string name]
{
get
{
int i = 0;
bool found = false;
for(i = 0;i < stu.length;i++)
{
if(stu.Name == name)
{
found = true;
break;
}
}
if(found)
{
return stu[i];
}
else
{
return null;
}
}
}
}MyCLass myclass = new MyCLass();
myclass["Zhu"].Name = "Huang";错误:未将对象引用设置到对象实例
{
//构造函数
public MyStudent(string name)
{
this.name = name;
} //name字段
private string name;
//属性
public string Name
{get;set;}
}
class MyClass
{
//构造函数
public MyCLass()
{
stu[0] = new Mystudent("Liu");
stu[1] = new Mystudent("Li");
stu[2] = new Mystudent("Zhu");
}
//声明3个对象
private MyStudent[] stu = new MyStudent[3];
//索引器
public MyStudent this[string name]
{
get
{
int i = 0;
bool found = false;
for(i = 0;i < stu.length;i++)
{
if(stu.Name == name)
{
found = true;
break;
}
}
if(found)
{
return stu[i];
}
else
{
return null;
}
}
}
}MyCLass myclass = new MyCLass();
myclass["Zhu"].Name = "Huang";错误:未将对象引用设置到对象实例
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
namespace Study
{
class Program
{
static void Main(string[] args)
{
ScoreIndex s = new ScoreIndex();
s["张三", 1] = 90;
s["张三", 2] = 100;
s["张三", 3] = 80;
s["李四", 1] = 60;
s["李四", 2] = 70;
s["李四", 3] = 50;
Console.WriteLine("张三课程编号为1的成绩为:" + s["张三",1]);
Console.WriteLine("张三的所有成绩为:");
ArrayList temp;
temp = s["张三"];
foreach (IndexClass b in temp)
{
Console.WriteLine("姓名:" + b.Name + "课程编号:" + b.CourseID + "分数:" + b.Score);
}
Console.ReadKey();
}
}
class IndexClass
{
private string _name;
private int _courseid;
private int _score;
public IndexClass(string _name, int _courseid, int _score)
{
this._name = _name;
this._courseid = _courseid;
this._score = _score;
}
public string Name
{
get { return _name; }
set { this._name = value; }
}
public int CourseID
{
get { return _courseid; }
set { this._courseid = value; }
}
public int Score
{
get { return _score; }
set { this._score = value; }
}
}
class ScoreIndex
{
private ArrayList arr;
public ScoreIndex()
{
arr = new ArrayList();
}
public int this[string _name, int _courseid]
{
get
{
foreach (IndexClass a in arr)
{
if (a.Name == _name && a.CourseID == _courseid)
{
return a.Score;
}
}
return -1;
}
set
{
arr.Add(new IndexClass(_name, _courseid, value)); //arr["张三",1]=90
}
}
//重载索引器
public ArrayList this[string _name]
{
get
{
ArrayList temp = new ArrayList();
foreach (IndexClass b in arr)
{
if (b.Name == _name)
{
temp.Add(b);
}
}
return temp;
}
}
}
}
private string name;
//属性
public string Name
{get;set;}
}//这样写的Name属性会生成单独的私有字段,和你前面 的private string name没一点关系了
//构造函数
public MyStudent(string name)
{
this.Name = name;
}
属性和字段是不一样的,如果你构造里是给字段赋值,那你属性里就得写明所使用的字段
二。改属性
//属性
public string Name
{
get{return this.name;}
set{this.name=value;}
}