目的:
有一个Student 类,我想让他在main里调用能实现:用名字调用而不是下标调用,如Stu["张三"].age=20;非Stu[0].age=20我自己写了下,出现问题
一、首先是Student类里我是这样写的
public class Student
{
public string Name;
public int Age; public Student(string name,int age)
{
this.Name = name;
this.Age = age;
}
public Student()
{
}
public Student[] Stu; public Student this[int index]
{
get { return Stu[index]; }
set { Stu[index] = value; }
}
public Student this[string name]
{
get
{
int i;
bool flag = false;
for (i = 0; i < Stu.Length; i++)
{
if (name == Stu[i].Name)
{
flag = true;
break;
}
}
if (flag)
{
return Stu[i];
}
else
{
return null;
}
}
}
}
二、Main类
public class Test
{
static void Main(string[] args)
{
Student stu = new Student();
stu[0] = new Student("a", GSex.Male, 25);
stu[1] = new Student("b", GSex.Male, 20);
stu[2] = new Student("z", GSex.Female, 50);
Console.WriteLine(stu["a"].Age);
Console.ReadLine();
}
}
在 set { Stu[index] = value; }处出现异常
三、问题一:
是不是我Student类里写错了,不用定义一个stu[]的数组
问题二:
请问应该怎么写,我只是想实现一开始提出的功能而已
谢谢
有一个Student 类,我想让他在main里调用能实现:用名字调用而不是下标调用,如Stu["张三"].age=20;非Stu[0].age=20我自己写了下,出现问题
一、首先是Student类里我是这样写的
public class Student
{
public string Name;
public int Age; public Student(string name,int age)
{
this.Name = name;
this.Age = age;
}
public Student()
{
}
public Student[] Stu; public Student this[int index]
{
get { return Stu[index]; }
set { Stu[index] = value; }
}
public Student this[string name]
{
get
{
int i;
bool flag = false;
for (i = 0; i < Stu.Length; i++)
{
if (name == Stu[i].Name)
{
flag = true;
break;
}
}
if (flag)
{
return Stu[i];
}
else
{
return null;
}
}
}
}
二、Main类
public class Test
{
static void Main(string[] args)
{
Student stu = new Student();
stu[0] = new Student("a", GSex.Male, 25);
stu[1] = new Student("b", GSex.Male, 20);
stu[2] = new Student("z", GSex.Female, 50);
Console.WriteLine(stu["a"].Age);
Console.ReadLine();
}
}
在 set { Stu[index] = value; }处出现异常
三、问题一:
是不是我Student类里写错了,不用定义一个stu[]的数组
问题二:
请问应该怎么写,我只是想实现一开始提出的功能而已
谢谢
public Student(string name,int age)
{
this.Name = name;
this.Age = age;
Stu = new Student[3];
} 这样你的代码可执行,但很显然,Stu被限制为3个大小,你需要改进这一点。
1:有多个Student对象的类应该是一个集合类,与单个对象的类放在一直这不合理,应当分离,设计出专门放Student对象集合的类,比如:
Students,Class等。
2:存储多个学生对象的数据结构,尽量不要选择数组。因为数组是固定长度的,对于长度未知或者可能变化的情况它无法适应,比较好的解决方案是使用泛型的List<T>列表。给个例子:public class Student
{
public string Name;
public int Age; public Student(string name,int age)
{
this.Name = name;
this.Age = age;
}
}public class Students
{
private List<Student> sts;
public Students()
{
sts = new List<Student>();
}
public void Add(Student s){sts.Add(s);}
public void Remove(Student s){sts.Remove(s);}
public Student this[string name]
{
get
{
foreach(Student s in sts)
{
if(s.Name == name)return s;
}
return null;
}
}
}在Main中使用时,这样: public class Test
{
static void Main(string[] args)
{
Students sts = new Students();
sts.Add(new Student("a", GSex.Male, 25));
sts.Add(new Student("b", GSex.Male, 20));
sts.Add(new Student("z", GSex.Female, 50)); Console.WriteLine(sts["a"].Age);
Console.ReadLine();
}
未经测试,大体就是这样,呵呵。
对了,我的main里面是这样写的static void Main(string[] args)
{
Student stu = new Student();
stu[0] = new Student("张三", 25);
stu[1] = new Student("李四", 20);
stu[2] = new Student("王五", 50);
Console.WriteLine(stu["张三"].Age);
Console.ReadLine();
}
public Student()
{
this.Name = “df”;
this.Age = 12;
}
public Student[] Stu;
——public Student[] Stu=new Student[7];
错误就是我说的,但是你要把:
Stu = new Student[3];这个同时放到两个构造函数中。 public Student(string name,int age)
{
this.Name = name;
this.Age = age;
Stu = new Student[3];
}
public Student()
{
Stu = new Student[3];
} 因为你在Main函数中调用的是:
Student stu = new Student();是无参构造,如果你只改上面那个构造函数,你的Stu仍然是null。
{
public string Name;
public int Age; public Student(string name, int age)
{
this.Name = name;
this.Age = age;
this.Stu = new Student[3];
}
public Student()
{
this.Stu = new Student[3];
}
public Student[] Stu; public Student this[int index]
{
get { return Stu[index]; }
set { Stu[index] = value; }
}
public Student this[string name]
{
get
{
int i;
bool flag = false;
for (i = 0; i < Stu.Length; i++)
{
if (name == Stu[i].Name)
{
flag = true;
break;
}
}
if (flag)
{
return Stu[i];
}
else
{
return null;
}
}
}
} class Program
{
static void Main(string[] args)
{
Student stu = new Student();
stu[0] = new Student("张三", 25);
stu[1] = new Student("李四", 20);
stu[2] = new Student("王五", 50);
Console.WriteLine(stu["张三"].Age);
Console.ReadLine(); }
}
using System;
using System.Collections.Generic;
using System.Text; namespace ConsoleApplication1
{
public class Student //索引器类
{
public string Name;//索引器属性
public int Age; //索引器属性 public Student(string name, int age)
{
this.Name = name;
this.Age = age;
}
public Student()//构造函数
{ }
public Student[] Stu=new Student[10];//用自己定义数组 public Student this[int index] //下表找元素
{
get { return Stu[index]; }
set { Stu[index] = value; }
}
//public Student this[string name]//名字找对象
//{
// get
// {
// int i;
// bool flag = false;
// for (i = 0; i < Stu.Length; i++)
// {
// if (name == Stu[i].Name)
// {
// flag = true;
// break;
// }
// }
// if (flag)
// {
// return Stu[i];
// }
// else
// {
// return null;
// }
// }
//}
public Student this[string age]//应该找不能重复的属性来作为下标值,为了演示,通过年龄找对象
{
get{
bool isok = false;
int i;
for ( i = 0; i < Stu.Length;i++ )
{
if (Stu[i]!=null&& Convert.ToInt32(age) == Stu[i].Age)
{
isok = true;
break;
} //else { return null; } }
if (isok)
{
return Stu[i];
}
else
{
return null;
}
}
set { Stu[Convert.ToInt32(age)] = value; }
}
}
public class Test
{
static void Main(string[] args)
{
Student stu = new Student();
stu[0] = new Student("a", 18);
stu[1] = new Student("b", 28);
stu[2] = new Student("z",38);
if (stu[(28).ToString()] != null) //如果不再此范围内则为空
{
Console.WriteLine(stu[(28).ToString()].Name);//对象不能为空
}
else
{
Console.WriteLine("为空!");
}
Console.ReadLine();
}
}
}