Interface demo: implements two interfaces

image_pdfimage_print

   

/*
Learning C# 
by Jesse Liberty

Publisher: O'Reilly 
ISBN: 0596003765
*/
 using System;

 namespace InterfaceDemo
 {
     interface IStorable
     {
         void Read();
         void Write(object obj);
         int Status { get; set; }

     }

     // here's the new interface
     interface ICompressible
     {
         void Compress();
         void Decompress();
     }


     // Document implements both interfaces
     class Document : IStorable, ICompressible
     {
         // the document constructor
         public Document(string s)
         {
             Console.WriteLine("Creating document with: {0}", s);

         }

         // implement IStorable
         public void Read()
         {
             Console.WriteLine(
                 "Implementing the Read Method for IStorable");
         }

         public void Write(object o)
         {
             Console.WriteLine(
                 "Implementing the Write Method for IStorable");
         }

         public int Status
         {
             get { return status; }
             set { status = value; }
         }

         // implement ICompressible
         public void Compress()
         {
             Console.WriteLine("Implementing Compress");
         }
 
         public void Decompress()
         {
             Console.WriteLine("Implementing Decompress");
         }


         // hold the data for IStorable's Status property
         private int status = 0;
     }


    public class TesterInterfaceDemo2
    {
       public void Run()
       {
           Document doc = new Document("Test Document");
           doc.Status = -1;
           doc.Read();
           doc.Compress();
           Console.WriteLine("Document Status: {0}", doc.Status);
       }

       [STAThread]
       static void Main()
       {
          TesterInterfaceDemo2 t = new TesterInterfaceDemo2();
          t.Run();
       }
    }
 }