zoukankan      html  css  js  c++  java
  • 虚对象和对象的多态

    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();
            }
        }
    }
  • 相关阅读:
    windows7 Cygwin 下安装 YouCompleteMe 插件
    挣值管理(EVT)
    关键路径法
    PERT(计划评审技术,Program Evaluation an Review Technique)
    孙子兵法--你如何来做好事情
    项目经理解决冲突的模式
    PMP考试--成本管理中常用的概念
    PMP考试--挣值如何计算?
    PMP-产品范围与项目范围区别
    PMP考试--价值工程法
  • 原文地址:https://www.cnblogs.com/shawnzxx/p/3022125.html
Copyright © 2011-2022 走看看