using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CShap_Test1
{
class Student
{
public string Name { get; set; }
public int Num { get; set; }
public Student()
:this("",0)
{
}
public Student(string name, int num)
{
Name = name;
Num = num;
}
virtual public void speak()
//声æ为
èå½æ°ï¼ä¸é¢çæ´¾ç类就å¯ä»¥
éè½½äº
{
Console.WriteLine("I am a student!");
}
public override string ToString()
//对Objectåºç±»æ¹æ³ToStringçéè½½
{
return string.Format("Name : {0} Num : {1}", Name, Num);
}
}
class Undergraduate : Student
{
public double Score { get; set; }
public Undergraduate(string name, int num, double score)
: base(name, num)
{
Score = score;
}
new public void speak()
//注æè¿éï¼newæ¯éè èä¸æ¯éè½½ 注æå¨
mainå½æ°éé¢å¤æå¤çæ¶åçè¾åº
{
Console.WriteLine("I am an undergraduated student!");
}
public override string ToString()
{
return base.ToString() + string.Format(" Score : {0}", Score);
}
}
class Graduate : Student
{
public bool Employeed { get; set; }
public Graduate(string name, int num, bool employeed)
: base(name, num)
{
Employeed = employeed;
}
public override void speak()
//éè½½åºç±»çspeakæ¹æ³
{
Console.WriteLine("I am a graduated student!");
}
public override string ToString()
{
return base.ToString() + string.Format(" Employeed : {0}", Employeed);
}
}
class Program
{
static void Main(string[] args)
{
Student s = new Student("Jack", 20101);
Console.WriteLine(s);
s.speak();
Console.WriteLine();
Undergraduate u = new Undergraduate("Marry", 20133, 89.2);
Console.WriteLine(u);
u.speak();
Console.WriteLine();
Graduate g = new Graduate("Tick", 20198, true);
Console.WriteLine(g);
g.speak();
Console.WriteLine("\nPolymophsim:");
//å¤å°å¤ç
Student[] p = new Student[3];
//studentæéå¯ä»¥æåæ´¾çç±»
p[0] = s;
p[1] = u;
p[2] = g;
foreach (var t in p)
{
t.speak();
Console.WriteLine(t);
}
}
}
}