using System;
using System.Collections.Generic;
using System.Text; namespace 大学生
{
public class gpas
{ //public int avg;
public gpas(int a)
{
this.avg = a;
}
public int avg
{
set
{
avg = value;
}
get
{
return avg;
}
}
}
public class arrylist {
gpas[] gpa;
public arrylist(int count)
{
gpa = new gpas[count];
}
public gpas this[int year]
{
get {
if(year<=0||year>4){
Console.WriteLine("年纪无效");
return null;
}
int index = year - 1;
return gpa[index];
}
set
{
if (year <= 0 || year > 4)
{
Console.WriteLine("年纪无效");
return ;
}
int index = year - 1;
gpa[index]=value;
} }
} public class Program
{
static void Main(string[] args)
{
arrylist alist = new arrylist(4);
gpas one = new gpas(88);
gpas two = new gpas( 78);
gpas three = new gpas( 89);
gpas four = new gpas( 88);
alist[0] = one;
alist[1] = two;
alist[2] = three;
alist[3] = four;
Console.WriteLine("请输入有效的年级");
int count = int.Parse(Console.ReadLine());
gpas gp = alist[count];
Console.WriteLine(gp.avg);
}
}
}
一调试,他就说在avg的读写属性哪报错 好像是递归?请高手指点,本人在线等,急急!!!!
using System.Collections.Generic;
using System.Text; namespace 大学生
{
public class gpas
{ //public int avg;
public gpas(int a)
{
this.avg = a;
}
public int avg
{
set
{
avg = value;
}
get
{
return avg;
}
}
}
public class arrylist {
gpas[] gpa;
public arrylist(int count)
{
gpa = new gpas[count];
}
public gpas this[int year]
{
get {
if(year<=0||year>4){
Console.WriteLine("年纪无效");
return null;
}
int index = year - 1;
return gpa[index];
}
set
{
if (year <= 0 || year > 4)
{
Console.WriteLine("年纪无效");
return ;
}
int index = year - 1;
gpa[index]=value;
} }
} public class Program
{
static void Main(string[] args)
{
arrylist alist = new arrylist(4);
gpas one = new gpas(88);
gpas two = new gpas( 78);
gpas three = new gpas( 89);
gpas four = new gpas( 88);
alist[0] = one;
alist[1] = two;
alist[2] = three;
alist[3] = four;
Console.WriteLine("请输入有效的年级");
int count = int.Parse(Console.ReadLine());
gpas gp = alist[count];
Console.WriteLine(gp.avg);
}
}
}
一调试,他就说在avg的读写属性哪报错 好像是递归?请高手指点,本人在线等,急急!!!!
解决方案 »
- .net using()相关
- VS2005出现未将对象引用设置到对象的实例的问题,求高手解答
- 急!使用DynamicResource的c#代码
- 在visual studio中连接数据库时提示权限不够?
- 如何判断两个dll内容一致
- Windows编程下如何在按钮单击事件里获得当前解决方案所在目录
- FreeText里的拾色器
- 循环向窗体添加控件,随机数是一样的,怎么回事.
- param关键字不能识别初始化列表吗? 有编译错误
- c# 怎么生成自定义控件, 怎么调用自定义控件
- GridView里面加了一个TemplateField,在ItemTemplate里面放了一个RadioButtonList.但不能帮定数据了
- Winform开发的用户控件通过IE发布时在客户端遇到的问题
public class gpas
{
private int _avg;
public gpas(int a)
{
this._avg= a;
}
public int avg
{
set
{
_avg = value;
}
get
{
return _avg;
}
}
}
建议你给属性名称更改一下,同时给变量赋值
using System.Collections.Generic;
using System.Text;namespace Test
{
public class People
{
private int _intAge;
public People(int intAge)
{
this._intAge = intAge;
}
public int Age
{
set { _intAge = value; }
get { return _intAge; }
}
} public class ArrList
{
People[] people;
public ArrList(int count)
{
people = new People[count];
}
public People this[int year]
{
get
{
if (year <= 0 && year > 4)
{
Console.WriteLine("年纪无效");
}
return people[year>0?year-1:1];
}
set
{
if (year <= 0 && year > 4)
{
Console.WriteLine("年纪无效");
}
people[year > 0 ? year - 1 : 1] = value;
}
}
}
public class Program
{
static void Main(string[] args)
{
ArrList list = new ArrList(4);
People p1 = new People(88);
People p2 = new People(78);
People p3 = new People(89);
People p4 = new People(88);
list[0] = p1;
list[1] = p2;
list[2] = p3;
list[3] = p4;
Console.WriteLine("请输入有效的年级");
People people = list[int.Parse(Console.ReadLine())];
Console.WriteLine(people.Age);
Console.ReadKey();
}
}
}