C# 繼承 建構子

class Student
{
    public string Name { get; set; }
    public int Math { get; set; }
    public int Eng { get; set; }
    public Student(string name, int math, int eng)
    {
        Name = name;
        Math = math;
        Eng = eng;
    }

    public virtual float Count()
    {
        return (Math + Eng) / 2.0f;
    }
}
    
class CollegeS : Student
{
    public CollegeS(string name, int math, int eng) : base(name, math, eng)
    {

    }
    public override float Count()
    {
        return Math + Eng;
    }
}

class Program
{
    static void Main(string[] args)
    {
        Student John = new Student("John",80,81);
        Console.WriteLine(John.Count());

        CollegeS Tom = new CollegeS("Tom", 80, 81);            
        Console.WriteLine(Tom.Count());
        Console.Read();
    }
}

發佈留言