콘솔을 자신의 클래스로 랩핑하고 대신 사용하십시오. 당신은 그것을 위해 상속 할 필요가 없습니다
class MyConsole
{
public static string ReadLine()
{
return System.Console.ReadLine();
}
public static string ReadLine(string message)
{
System.Console.WriteLine(message);
return ReadLine();
}
// add whatever other methods you need
}
그런 다음 당신이 앞서 갈 수있는 대신 프로그램에서 하나를 사용
string whatEver = MyConsole.ReadLine("Type something useful:");
당신이 조금 더 밀어 및 MyConsole
을하고자하는 경우 좀 더 유연하게, 당신은 또한 입력/출력 구현을 대체하기 위해 지원을 추가 할 수 있습니다 : 이것은 당신이 어떤 TextReader
implemen에서 프로그램을 구동 할 수있는 것
class MyConsole
{
private static TextReader reader = System.Console.In;
private static TextWriter writer = System.Console.Out;
public static void SetReader(TextReader reader)
{
if (reader == null)
{
throw new ArgumentNullException("reader");
}
MyConsole.reader = reader;
}
public static void SetWriter(TextWriter writer)
{
if (writer == null)
{
throw new ArgumentNullException("writer");
}
MyConsole.writer = writer;
}
public static string ReadLine()
{
return reader.ReadLine();
}
public static string ReadLine(string message)
{
writer.WriteLine(message);
return ReadLine();
}
// and so on
}
명령이 멋진 자동화 시나리오를 제공 할 수 대신 콘솔의 파일, ...
업데이트에서 올 수 있도록 tation는
이 노출해야하는 대부분의 방법은 매우 간단합니다. 좋아, 아마 쓰는 것이 약간 지루하지만 일의 많은 분이 아니기 때문에 한 번만해야합니다.
public static void WriteLine()
{
writer.WriteLine();
}
public static void WriteLine(string text)
{
writer.WriteLine(text);
}
public static void WriteLine(string format, params object args)
{
writer.WriteLine(format, args);
}
당신이 방금 콘솔에서 작동 자신의 정적 클래스를 만들 수 없습니다 :
예 (우리가 할당 독자와 작가로, 위의 내 두 번째 샘플에있는 가정)? –