Interface demo

image_pdfimage_print

   

/*
Learning C# 
by Jesse Liberty

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

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

     }

     // create a Document class that implements the IStorable interface
     class Document : IStorable
     {
         public Document(string s)
         {
             Console.WriteLine("Creating document with: {0}", s);
         }

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

         // implement the Write method
         public void Write(object o)
         {
             Console.WriteLine(
                 "Implementing the Write Method for IStorable");
         }
         // implement the property
         public int Status
         {
             get{ return status; }
             set{ status = value; }
         }

         // store the value for the property
         private int status = 0;
     }

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

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