⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ex-08-01

📁 Programming Csharp Source Code(代码) Programming Csharp Source Code
💻
字号:
//Example 08-01: Using a simple interface

using System;

// declare the interface
interface IStorable
{
   // no access modifiers, methods are public
   // no implmentation
   void Read();
   void Write(object obj);
   int Status { get; set; }

}

// create a class which implements the IStorable interface
public 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;
}

// Take our interface out for a spin
public class Tester
{
 
   static void Main()
   {
      // access the methods in the Document object
      Document doc = new Document("Test Document");
      doc.Status = -1;
      doc.Read();
      Console.WriteLine("Document Status: {0}", doc.Status); 

      // cast to an interface and use the interface
      IStorable isDoc = (IStorable) doc;
      isDoc.Status = 0;
      isDoc.Read();
      Console.WriteLine("IStorable Status: {0}", isDoc.Status);

   }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -