如何在main函数中实例化一个类中的私有数组字段
class Teacher{
private int tecId;
private string tecName;
private string dept;
private Course [ ]course;//在main函数中实例化
} --------------------编程问答--------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Course
{
}
class Teacher
{
private int tecId;
private string tecName;
private string dept;
private Course[] course;
public void Debug() { Console.WriteLine(course != null); }
}
class Program
{
static void Main(string[] args)
{
Teacher t = new Teacher();
t.Debug();
t.GetType().GetField("course", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic).SetValue(t, new Course[] { });
t.Debug();
}
}
}
False
True
Press any key to continue . . . --------------------编程问答-------------------- 再请教一下,如果我定义的是一个Teacher[]呢?请赐教 --------------------编程问答--------------------
new Course[]
修改成
new Teacher[] --------------------编程问答-------------------- 用反射太高端了,这种后门少些才好
--------------------编程问答-------------------- 大神们,帮我看看这个问题出在哪吧,每次运行到teacher[i].Course[i].CourseName = Console.ReadLine();就出错。
class Teacher
{
private int tecId;
private string tecName;
private string dept;
private Course[] course;
public teacher(Course[] _course)
{
this.course = _couse;
}
}
class Program
{
static void Main(string[] args)
{
Teacher t = new Teacher(new Course[3]);
}
}
源代码:
-------------------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace exam3
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("请输入教师人数:");
int n = Convert.ToInt32(Console.ReadLine());
Teacher[] teacher = new Teacher[n];
int i,j;
for (i = 0; i < n; i++)
{
Console.WriteLine("请输入第{0}个教师信息:",i+1);
Console.WriteLine("请输入教师工号:");
int q = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("请输入教师姓名:");
string s = Console.ReadLine();
teacher[i] = new Teacher(q, s, 5);
Console.WriteLine("请输入教师所教第{0}门课程名:", i + 1);
teacher[i].Course[i].CourseName = Console.ReadLine();//运行到此处就出错了
Console.WriteLine("请输入教师所教第{0}门课程学时:", i + 1);
teacher[i].Course[i].ClassHour = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("请输入教师所属系别:");
teacher[i].Dept = Console.ReadLine();
Console.WriteLine("请输入教师所教课程:");
Console.WriteLine("请输入教师所教课程门数:");
//for (j = 0; j < courseTotal[i]; j++)
//{
// Console.WriteLine("请输入教师所教第{0}门课程名:", j + 1);
// teacher[i].Course[j].CourseName = Console.ReadLine();
// Console.WriteLine("请输入教师所教第{0}门课程学时:", j + 1);
// teacher[i].Course[j].ClassHour = Convert.ToInt32(Console.ReadLine());
//}
}
Console.WriteLine("教师信息如下:");
Console.WriteLine("工号\t\t姓名\t\t课程名\t\t学时");
//for (i = 0; i < n; i++)
//{
// Console.Write(teacher[i].TecId + "\t\t");
// Console.Write(teacher[i].TecName + "\t\t");
// Console.WriteLine();
// for (j = 0; j < courseTotal[i];j++ )
// Console.WriteLine(" \t\t \t\t" + teacher[i].Course[j].CourseName);
// Console.WriteLine();
//}
}
}
}
-------------------------------------------------------------------------------------
Teacher.cs
-------------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace exam3
{
class Course
{
private string courseName;
private int classHour;
public string CourseName
{
get;
set;
}
public int ClassHour
{
get;
set;
}
}
class Teacher
{
private int tecId;
private string tecName;
private string dept;
private Course[] course;
public Teacher(int x, string y, int count)
{
tecId = x;
tecName = y;
course = new Course[count];
}
public Teacher()
{
}
public int TecId
{
get;
set;
}
public string TecName
{
get;
set;
}
public Course[] Course
{
get;
set;
}
public string Dept
{
get;
set;
}
}
}
--------------------编程问答-------------------- teacher[i].Course = Enumerable.Range(0, i).Select(x => new Course()).ToArray();
teacher[i].Course[i].CourseName = Console.ReadLine();//运行到此处就出错了
补充:.NET技术 , C#