students=new Student[]{
new Student("01","张三","男","一班","一年级","计算机学院"),
new Undergraduate("02", "李四", "男", "一班", "一年级","计算机学院"),
new Graduate("03", "王五", "男", "一班", "一年级", "计算机学院","钱一")};
它们定义string类型的自动属性Department,表示学生所在的院系;再为Graduate定义string类型的自动属性Tutor,表示研究生的导师。
当students作为参数传递的时候,如何分别显示students[0],students[1],students[2]的信息,if(students[0] is Graduate)是能判断是哪个类型,但是判断后也不能显示信息
7 个解决方案
#1
如果你这几个属性石子类的属性,那么你需要转型才能访问,或者通过反射去取值
#2
在Student类里定义一个virtual f();子类覆盖
#3
需要强制转换一下
if(students[0] is Graduate)
{
((Graduate)students[0]).Tutor
}
if(students[0] is Graduate)
{
((Graduate)students[0]).Tutor
}
#4
++
#5
用索引器会方便一点
public class Students
{
public Undergraduat this[Undergraduat o]
{
get
{
if(o ==null)
{
return null;
}
else
return o;
}
}
public Graduate this[Graduate o]
{
get
{
if(o ==null)
{
return null;
}
else
return o;
}
}
}
Students s=new Students();
Undergraduate u= new Undergraduate("02", "李四", "男", "一班", "一年级","计算机学院"),
Graduate g= new Graduate("03", "王五", "男", "一班", "一年级", "计算机学院","钱一")};
s[u]=u;
s[g]=g;
你在用的时候属性直接在里面就显示了;
手打,忘采纳...
public class Students
{
public Undergraduat this[Undergraduat o]
{
get
{
if(o ==null)
{
return null;
}
else
return o;
}
}
public Graduate this[Graduate o]
{
get
{
if(o ==null)
{
return null;
}
else
return o;
}
}
}
Students s=new Students();
Undergraduate u= new Undergraduate("02", "李四", "男", "一班", "一年级","计算机学院"),
Graduate g= new Graduate("03", "王五", "男", "一班", "一年级", "计算机学院","钱一")};
s[u]=u;
s[g]=g;
你在用的时候属性直接在里面就显示了;
手打,忘采纳...
#6
强制类型转换是最简单的方法,
#7
多态~~~~~~~~~~~~~~~~~~~
#1
如果你这几个属性石子类的属性,那么你需要转型才能访问,或者通过反射去取值
#2
在Student类里定义一个virtual f();子类覆盖
#3
需要强制转换一下
if(students[0] is Graduate)
{
((Graduate)students[0]).Tutor
}
if(students[0] is Graduate)
{
((Graduate)students[0]).Tutor
}
#4
++
#5
用索引器会方便一点
public class Students
{
public Undergraduat this[Undergraduat o]
{
get
{
if(o ==null)
{
return null;
}
else
return o;
}
}
public Graduate this[Graduate o]
{
get
{
if(o ==null)
{
return null;
}
else
return o;
}
}
}
Students s=new Students();
Undergraduate u= new Undergraduate("02", "李四", "男", "一班", "一年级","计算机学院"),
Graduate g= new Graduate("03", "王五", "男", "一班", "一年级", "计算机学院","钱一")};
s[u]=u;
s[g]=g;
你在用的时候属性直接在里面就显示了;
手打,忘采纳...
public class Students
{
public Undergraduat this[Undergraduat o]
{
get
{
if(o ==null)
{
return null;
}
else
return o;
}
}
public Graduate this[Graduate o]
{
get
{
if(o ==null)
{
return null;
}
else
return o;
}
}
}
Students s=new Students();
Undergraduate u= new Undergraduate("02", "李四", "男", "一班", "一年级","计算机学院"),
Graduate g= new Graduate("03", "王五", "男", "一班", "一年级", "计算机学院","钱一")};
s[u]=u;
s[g]=g;
你在用的时候属性直接在里面就显示了;
手打,忘采纳...
#6
强制类型转换是最简单的方法,
#7
多态~~~~~~~~~~~~~~~~~~~