10 June 2016

Abstract Class C#

Abstract Class
Abstract C# 2016
using System;
using static System.Environment;

namespace AbstractSample
{
    // Define an abstract class
    public abstract class PdaItem
    {
        public PdaItem(string name)
        {
            Name = name;
        }

        public virtual string Name { get; set; }
        public abstract string GetSummary();
    }
    public class Contact : PdaItem
    {
        public Contact(string name)
            : base(name)
        {
        }

        public override string Name
        {
            get
            {
                return $"{ FirstName } { LastName }";
            }

            set
            {
                string[] names = value.Split(' ');
                // Error handling not shown.
                FirstName = names[0];
                LastName = names[1];
            }
        }

        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Address { get; set; }

        public override string GetSummary()
        {
            return $"FirstName: { FirstName + NewLine }"
            + $"LastName: { LastName + NewLine }"
            + $"Address: { Address + NewLine }";
        }

        // ...
    }

    public class Appointment : PdaItem
    {
        public Appointment(string name) :
            base(name)
        {
            Name = name;
        }

        public DateTime StartDateTime { get; set; }
        public DateTime EndDateTime { get; set; }
        public string Location { get; set; }

        // ...
        public override string GetSummary()
        {
            return $"Subject: { Name + NewLine }"
                + $"Start: { StartDateTime + NewLine }"
                + $"End: { EndDateTime + NewLine }"
                + $"Location: { Location }";
        }
    }

    public class Program
    {
        public static void Main()
        {
            PdaItem[] pda = new PdaItem[3];

            Contact contact = new Contact("Sherlock Holmes");
            contact.Address = "221B Baker Street, London, England";
            pda[0] = contact;

            Appointment appointment =
               new Appointment("Soccer tournament");
            appointment.StartDateTime = new DateTime(2008, 7, 18);
            appointment.EndDateTime = new DateTime(2008, 7, 19);
            appointment.Location = "Estádio da Machava";
            pda[1] = appointment;

            contact = new Contact("Anne Frank");
            contact.Address =
                "Apt 56B, Whitehaven Mansions, Sandhurst Sq, London";
            pda[2] = contact;

            List(pda);
        }

        public static void List(PdaItem[] items)
        {
            // Implemented using polymorphism. The derived
            // type knows the specifics of implementing 
            // GetSummary().
            foreach (PdaItem item in items)
            {
                Console.WriteLine("________");
                Console.WriteLine(item.GetSummary());
            }

            Console.ReadKey();
        }
    }
}

0 nhận xét:

Post a Comment

 

BACK TO TOP

Xuống cuối trang