[英]Interface for two methods that take different parameters
可能要問的問題不大,但我希望有人能在這里幫助我,因為我完全迷路了。 我有一項工作要做的工作(我是一名研究生程序員,他們給我帶來了一種“挑戰”)-別擔心,我不是要一個完整的答案,這也不是我每天要做的事情日常工作,有點額外。 但是我認為它涉及到接口,當涉及到接口時我完全迷失了,所以想知道是否有人可以指出我正確的方向。
基本上,我得到了一個注冊表遍歷器,它可以遍歷注冊表並根據給定的參數在控制台應用程序上打印出鍵和值(有關代碼,請參見下文)
class RegistryList
{
public void RegistryWalker(RegistryKey _key, int _indent)
{
Output.RegistryOutPut(_indent, String.Format("Key: {0}", _key.Name.Split('\\').Last()));
string[] straValues = _key.GetValueNames();
foreach (string strValue in straValues)
{
RegistryValueKind kind = _key.GetValueKind(strValue);
Output.RegistryOutPut(_indent + 1, String.Format("Value: {0}", strValue));
}
string[] straSubKeys = _key.GetSubKeyNames();
foreach (string strSubKey in straSubKeys)
{
try
{
RegistryKey subKey = _key.OpenSubKey(strSubKey);
RegistryWalker(subKey, _indent + 2);
Thread.Sleep(200);
}
catch (System.Security.SecurityException)
{
Console.WriteLine("Denied Access");
}
}
}
我必須為文件准備一個類似的文件,我已經完成了(再次參見下文)
class FileList
{
public void FileWalker()
{
StringCollection log = new StringCollection();
string[] drives = Environment.GetLogicalDrives();
foreach (string dr in drives)
{
DriveInfo di = new DriveInfo(dr);
if (!di.IsReady)
{
Console.WriteLine("{0} could not be read", di.Name);
continue;
}
DirectoryInfo rootDir = di.RootDirectory;
Output.FileOutput(rootDir);
Console.WriteLine("Files with restricted access:");
foreach (string s in log)
{
Console.WriteLine(s);
}
Console.WriteLine("Press any key");
Console.ReadKey();
}
}
}
為了記錄在案,上面代碼片段中的Output.RegistryOutput和Output.FileOutput調用了單獨的輸出函數(它們工作正常,它們打印出每個注冊表項和文件路徑,但我都需要一個函數-請參見下面的注釋)。
該任務的最后一部分是將遞歸放入一個單獨的函數中,該函數將采用FileWalker實例或RegistryWalker實例。 工作中的人將我引向接口以解決問題,但我完全迷失了。 這兩個函數將采用不同的參數,因此我看不到單個接口的工作方式,因為如果要使用注冊表遍歷器,則它需要一個RegistryKey和int值,但是文件遍歷器沒有參數。 我已經創建了一個IWalker界面(再次在下面),但是我看不到這對兩個功能如何起作用。
interface IWalker
{
void Walker();
}
誰能為我闡明這一點? 即使這是正確的方法,但我已經迷失了很長時間,並且在可能的情況下肯定需要一些指導。
謝謝!
看一看訪客模式,希望對您有所幫助。 http://en.wikipedia.org/wiki/Visitor_pattern
我認為這樣的最簡單方法
interface IWalker
{
void Walker(object a=null, object b=null);
}
class RegistryList: IWalker
{
public void Walker(object a, object b){
var _key = (RegistryKey)a;
var _indent = Convert.ToInt32(b)
RegistryWalker(_key, _indent)
}
private void RegistryWalker(RegistryKey _key, int _indent)
{
....
}
}
class FileListIWalker
{
public void Walker(object a=null, object b=null){
FileWalker();
}
public void FileWalker(){...}
{
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.