如题
解决方案 »
- vs服务器和iis服务器的问题!急急急急急急急急
- 请教一个关于Repeater的用法
- window.onresize 的问题
- asp.net 怎么调用delphi 制作的activex控件
- 怎么用UML表示文件夹和文件的关系
- 在asp.net中如何应用directx 9.0画一个立方体。。。
- 怎么用oracle long raw 的类型 存取文件(文件是 Word和Excel)就行
- GridView变形,页面一刷新,就变形
- vs2012 localdb 中文乱码问题
- asp.net中 UpdatePannel中使用fileUplord 的一个问题
- Visual Source Safe是不是源代码管理工具?Visual Studio 2008中有这个工具吗?
- 怎么让cs里产生的TextBox触发日期脚本事件?
下面的示例演示了接口实现。在此例中,接口 IPoint 包含属性声明,后者负责设置和获取字段的值。Point 类包含属性实现。
// keyword_interface_2.cs
// Interface implementation
using System;
interface IPoint
{
// Property signatures:
int x
{
get;
set;
} int y
{
get;
set;
}
}class Point : IPoint
{
// Fields:
private int _x;
private int _y; // Constructor:
public Point(int x, int y)
{
_x = x;
_y = y;
} // Property implementation:
public int x
{
get
{
return _x;
} set
{
_x = value;
}
} public int y
{
get
{
return _y;
}
set
{
_y = value;
}
}
}class MainClass
{
static void PrintPoint(IPoint p)
{
Console.WriteLine("x={0}, y={1}", p.x, p.y);
} static void Main()
{
Point p = new Point(2, 3);
Console.Write("My Point: ");
PrintPoint(p);
}
}
interface IPoint
{
// Property signatures:
int x
{
get;
set;
} int y
{
get;
set;
}
}class Point : IPoint
{
// Fields:
private int _x;
private int _y; // Constructor:
public Point(int x, int y)
{
_x = x;
_y = y;
} // Property implementation:
public int x
{
get
{
return _x;
} set
{
_x = value;
}
} public int y
{
get
{
return _y;
}
set
{
_y = value;
}
}
}class MainClass
{
static void PrintPoint(IPoint p)
{
Console.WriteLine("x={0}, y={1}", p.x, p.y);
} static void Main()
{
Point p = new Point(2, 3);
Console.Write("My Point: ");
PrintPoint(p);
}
}
{
void do();
}
class class1:I1
{
public void do()
{
Console.WriteLine("我是第一个人");
}
}
class class2:I1
{
public void do()
{
Console.WriteLine("第二");
}
}class MainClass
{
static void PrintPoint(IPoint p)
{
Console.WriteLine("我有什么用?");
} static void Main()
{
I1 obj_I1 =new class1();
obj_I1.do();
obj_I1 = new class2();
obj_I1.do();
}
{
static void PrintPoint(IPoint p)
{
Console.WriteLine("我有什么用?");
}
// 是引用楼上 几层那 代码。忽略吧
Execute();
}这个执行器接口可以实现为:
class SqlExecutor:IExecutor
或者
class FileExecutor:IExecutor他们都能执行,但是可能一个事执行SQL另一个则是执行文件。对于框架设计来说这点很重要,框架只需要知道我需要执行,但是到底怎么执行则有实现者来提供。
namespace Cipher
{
public interface ICipher
{
//加密
string encode(string str);
//解密
string decode(string str);
}
}
namespace Cipher
{
public interface ICipher
{
//加密
string encode(string str);
//解密
string decode(string str);
}
}下面两个类实现了Icipher。view plaincopy to clipboardprint?
/*简单的易位代码,通过将每个字符都向高位一定一位来加密字符串*/
public class SimpleCipher:ICipher
{
public string encode(string str)
{
string ciphertext = "";
for (int i = 0; i < str.Length; ++i)
{
Console.WriteLine(i.ToString()+" "+(str[i] + 1).ToString());
ciphertext = ciphertext + (char)(str[i] + 1);
}
return ciphertext;
}
public string decode(string str)
{
string plaintext = "";
for (int i = 0; i < str.Length; ++i)
{
plaintext = plaintext + (char)(str[i] - 1);
}
return plaintext;
}
/*简单的易位代码,通过将每个字符都向高位一定一位来加密字符串*/
public class SimpleCipher:ICipher
{
public string encode(string str)
{
string ciphertext = "";
for (int i = 0; i < str.Length; ++i)
{
Console.WriteLine(i.ToString()+" "+(str[i] + 1).ToString());
ciphertext = ciphertext + (char)(str[i] + 1);
}
return ciphertext;
}
public string decode(string str)
{
string plaintext = "";
for (int i = 0; i < str.Length; ++i)
{
plaintext = plaintext + (char)(str[i] - 1);
}
return plaintext;
} view plaincopy to clipboardprint?
/*通过将每个字符与一个16位的密钥进行XOR(异或)运算加密字符串*/
public class BitCipher : ICipher
{
ushort key;
public BitCipher(ushort k)
{
key = k;
}
public string encode(string str)
{
string ciphertext = "";
for (int i = 0; i < str.Length; ++i)
{
ciphertext = ciphertext + (char)(str[i] ^ key);
}
return ciphertext;
}
public string decode(string str)
{
string plaintext = "";
for (int i = 0; i < str.Length; ++i)
{
plaintext = plaintext + (char)(str[i] ^ key);
}
return plaintext;
}
/*通过将每个字符与一个16位的密钥进行XOR(异或)运算加密字符串*/
public class BitCipher : ICipher
{
ushort key;
public BitCipher(ushort k)
{
key = k;
}
public string encode(string str)
{
string ciphertext = "";
for (int i = 0; i < str.Length; ++i)
{
ciphertext = ciphertext + (char)(str[i] ^ key);
}
return ciphertext;
}
public string decode(string str)
{
string plaintext = "";
for (int i = 0; i < str.Length; ++i)
{
plaintext = plaintext + (char)(str[i] ^ key);
}
return plaintext;
} 虽然SimpleCipher和BitCipher在实现上完全不同,但他们都实现了ICipher接口。下面示范了两个类的用法:view plaincopy to clipboardprint?
static void Main(string[] args)
{
ICipher ciphRef;
BitCipher bit = new BitCipher(27);
SimpleCipher sc = new SimpleCipher();
string plain;
string coded;
ciphRef = sc;
Console.WriteLine("Using simple cipher.");
plain = "testing";
coded = ciphRef.encode(plain);
Console.WriteLine("Cipher text:"+coded);
plain = ciphRef.decode(coded);
Console.WriteLine("Plain text:"+plain);
ciphRef = bit;
Console.WriteLine("\nUsing bitwise cipher.");
plain = "testing";
coded = ciphRef.encode(plain);
Console.WriteLine("Cipher text:"+coded);
plain = ciphRef.decode(coded);
Console.WriteLine("Plain text:"+plain);
}
static void Main(string[] args)
{
ICipher ciphRef;
BitCipher bit = new BitCipher(27);
SimpleCipher sc = new SimpleCipher(); string plain;
string coded; ciphRef = sc;
Console.WriteLine("Using simple cipher.");
plain = "testing";
coded = ciphRef.encode(plain);
Console.WriteLine("Cipher text:"+coded); plain = ciphRef.decode(coded);
Console.WriteLine("Plain text:"+plain);
ciphRef = bit;
Console.WriteLine("\nUsing bitwise cipher.");
plain = "testing";
coded = ciphRef.encode(plain);
Console.WriteLine("Cipher text:"+coded);
plain = ciphRef.decode(coded);
Console.WriteLine("Plain text:"+plain);
} 创建加密接口的好处在于,无论加密方法如何实现,只要类中实现了该接口,就可以以相同的方式访问它。
我的博客:http://blog.csdn.net/kdmhh/archive/2009/08/23/4474387.aspx