C#中基類的重寫

iamzxf發表於2015-04-07

基類中需要重寫的方法和屬性設定為virtual,而在繼承類中將相應的屬性或方法設定為override。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace chap4_3
{
    class Employee
    {
        public string Name { get; set; }
        public int Age { get; set; }
        public double Salary { get; set; }

        public Employee(string name, int age, double salary)
        {
            this.Name = name;
            this.Age = age;
            this.Salary = salary;
        }

        public virtual void Disp()
        {
            Console.WriteLine("{0},{1},{2}",this.Name,this.Age,this.Salary);
        }
    }

    class Manager : Employee
    {
        public double Bonus { get; set; }
        public Manager(string name, int age, double salary, double bonus):base(name,age,salary)
        {
            this.Bonus = bonus;
        }

        public override void Disp()
        {
            Console.WriteLine("{0},{1},{2}", this.Name, this.Age, this.Salary+this.Bonus);
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Manager mng = new Manager("zxf", 38, 4000, 1000);
            mng.Disp();
            Console.ReadLine();
        }
    }
}



相關文章