ex-08-01
来自「Programming Csharp Source Code(代码) Prog」· 代码 · 共 76 行
TXT
76 行
//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 + =
减小字号Ctrl + -
显示快捷键?