using System;
using System.Collections.Generic;
using System.Text;namespace ConsoleApplication1
{
class Program
{
public class Person //定义人的属性,包括 年龄和身高
{
private int age;
private float hight;
public int Age
{
get { return Age; }
set { Age = value; }
}
public float Hight
{
get { return Hight; }
set { Hight = value; }
} }
public class Team //定义小组树形,每个小组包含5个人
{
private Person [] person = new Person[5];
public Person this[int index]
{
get { return person[index]; }
}
} private Team[] team=new Team[3]; //定义3个小组 static void Main(string[] args)
{
// 想在主程序中对任何一个小组的任何一个人的任何一种属性读取。
// ***************************** // 第i个小组的第j个人的年龄是i+j+1;
// ***************************** // 如何写代码?
}
}
}
using System.Collections.Generic;
using System.Text;namespace ConsoleApplication1
{
class Program
{
public class Person //定义人的属性,包括 年龄和身高
{
private int age;
private float hight;
public int Age
{
get { return Age; }
set { Age = value; }
}
public float Hight
{
get { return Hight; }
set { Hight = value; }
} }
public class Team //定义小组树形,每个小组包含5个人
{
private Person [] person = new Person[5];
public Person this[int index]
{
get { return person[index]; }
}
} private Team[] team=new Team[3]; //定义3个小组 static void Main(string[] args)
{
// 想在主程序中对任何一个小组的任何一个人的任何一种属性读取。
// ***************************** // 第i个小组的第j个人的年龄是i+j+1;
// ***************************** // 如何写代码?
}
}
}
解决方案 »
- 怎么回事,不同的ID读卡器读出来的信息不一样?
- C# Winform 控件层叠透明
- 这样删除提示类型不匹配
- 一个封锁操作被对 WSACancelBlockingCall 的调用中断
- c# Winform Listview的LargeIcon模式时内部拖动Items的实现问题
- C#如何取模呀?(这是pascal 的写法: 1 mod 2 = 1)
- 如何得到线程的类名 。在线等
- 急,请教调用已存在excel文件的问题?
- 导出EXCEL文件的问题
- DirectoryEntry 对ActiveDirectory操作,用Rename方法和Moveto方法。有点问题!在线等
- 请教treeview的结点删除后,要把结点的TAG属性对应的对象一起删掉,该怎么做?对象不知道类型,急,谢谢
- csdn开张 送分帖
Class cl = new Class();
person a = cl[1];这个样子的.
只是准备了一个能放5个Person的数组,并没有创建Person本身。所有Perseon this[index]返回的都是null。比如你可以改成这样:
private Person [] person = new Person[]{new Person(), new Person(), new Person(), new Person(), new Person()};同样道理(换成static,因为要从static Main中读取):
static private Team[] team=new Team[]{new Team(),new Team(),new Team()}; 然后就简单了:
static void Main(string[] args)
{
team[2][3].Age = 2 + 3;
}
{
for (int i = 0; i < 3; i++)
for (int j = 0; j < 5; j++)
{
team[i][j].Age = i + j + 1;
team[i][j].Hight = 170;
}
}
{
class Program
{
public class Person
{
private int age;
private float hight;
public int Age
{
get { return Age; }
set { Age = value; }
}
public float Hight
{
get { return Hight; }
set { Hight = value; }
} public Person()
{
} public Person(int age, float hight)
{
this.age = age;
this.hight = hight;
}
}
public class Team //定义小组树形,每个小组包含5个人
{
private Person[] person; public Team(Person [] person)
{
this.person = person;
} public Person this[int index]
{
get { return person[index]; }
}
} static void Main(string[] args)
{
Person[] persons = new Person[2];
persons[0] = new Person(9, 1.2f);
persons[1] = new Person(10, 1.3f);
Team[] team = new Team[3];
team[0] = new Team(persons);
}
}
}
比如让Team继承List:
public class Team:List<Person> //
{
....
}
这样,直接使用Team[index].Age了。