📄 ex-08-03
字号:
//Example 08-03: Using the is operator
using System;
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 IStorable
public class Document : IStorable
{
public Document(string s)
{
Console.WriteLine(
"Creating document with: {0}", s);
}
// IStorable.Read
public void Read()
{
Console.WriteLine(
"Implementing the Read Method for IStorable");
}
// IStorable.Write
public void Write(object o)
{
Console.WriteLine(
"Implementing the Write Method for IStorable");
}
// IStorable.Status
public int Status
{
get
{
return status;
}
set
{
status = value;
}
}
private int status = 0;
}
public class Tester
{
static void Main()
{
Document doc = new Document("Test Document");
// only cast if it is safe
if (doc is IStorable)
{
IStorable isDoc = (IStorable) doc;
isDoc.Read();
}
// this test will fail
if (doc is ICompressible)
{
ICompressible icDoc = (ICompressible) doc;
icDoc.Compress();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -