虚对象和对象的多态

Virtual memebers:

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

namespace ConsoleApplication2
{
    class Person
    {
        public string Firstname { get; set; }
        public string SecondName { get; set; }
        public virtual void Display()
        {
            Console.WriteLine("Person - {0}, {1}", Firstname, SecondName);
        }
        public virtual int CalculateYearIncome(int year)
        {
            if (year < 2000)
            {
                return 50000;
            }
            return 10000;
        }
    }
    class Employee : Person
    {
        public int YearIncome { get; set; }
        public override void Display()
        {
            Console.WriteLine("Employee - {0}, {1}, {2}", Firstname, SecondName, YearIncome);
        }
        public override int CalculateYearIncome(int year)
        {
            return 1000;
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            List<Person> myList = new List<Person>();
            Person p1 = new Person();
            p1.Firstname = "Nelson";
            p1.SecondName = "LaQuest";
            Employee e1 = new Employee();
            e1.Firstname = "whoa";
            e1.SecondName = "hey";
            e1.YearIncome = 23;
            myList.Add(p1);
            myList.Add(e1);

            foreach (var item in myList)
            {
                item.Display();
                Console.WriteLine("Year income of 2004 -> {0}", item.CalculateYearIncome(2004));
            }
            Console.ReadKey();

        }
        

        static string GetString(string prompt)
        {
            Console.Write("{0}>", prompt);
            return Console.ReadLine();
        }
    }
}

Polymorphism:

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

namespace ConsoleApplication2
{
    class Person
    {
        public string Firstname { get; set; }
        public string SecondName { get; set; }
        public virtual void Display()
        {
            Console.WriteLine("Person - {0}, {1}", Firstname, SecondName);
        }
    }
    class Employee : Person
    {
        public int YearIncome { get; set; }
        public override void Display()
        {
            Console.WriteLine("Employee - {0}, {1}, {2}", Firstname, SecondName, YearIncome);
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            List<Person> myList = new List<Person>();

            for (int i = 0; i < 3; i++)
            {
                myList.Add(GetPersonInput());
            }
            foreach (var item in myList)
            {
                item.Display();
            }

            Console.ReadKey();

        }
        static Person GetPersonInput()
        {
            while (true)
            {
                Console.WriteLine("[1] - Person\n[2] - Employee");
                int choice = int.Parse(Console.ReadLine());

                if (choice == 1)
                {
                    Person person = new Person();
                    person.Firstname = GetString("First Name");
                    person.SecondName = GetString("Second Name");
                    return person;
                }
                else if (choice == 2)
                {
                    Employee employee = new Employee();
                    employee.Firstname = GetString("First Name");
                    employee.SecondName = GetString("Second name");
                    Console.WriteLine("Year of Income> ");
                    employee.YearIncome = int.Parse(Console.ReadLine());
                    return employee;
                }
            }
            
        }

        static string GetString(string prompt)
        {
            Console.Write("{0}>", prompt);
            return Console.ReadLine();
        }
    }
}
原文地址:https://www.cnblogs.com/shawnzxx/p/3022125.html