728x90
반응형
추상클래스
using @interface;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace @interface
{
interface IRunnable
{
void Run();
}
interface IFlyable
{
void Fly();
}
class FlyingCar : IRunnable, IFlyable
{
public void Run()
{
Console.WriteLine("Run! Run!");
}
public void Fly()
{
Console.WriteLine("Fly! Fly!");
}
}
interface ILogger
{
void WriteLog(string message);
}
interface IFormattableLogger : ILogger
{
void WriteLog(string format, params object[] args);
}
class ConsoleLogger2 : IFormattableLogger
{
public void WriteLog(string message)
{
Console.WriteLine("{0} {1}", DateTime.Now.ToLocalTime(), message);
}
public void WriteLog(string format, params object[] args)
{
String message = String.Format(format, args);
Console.WriteLine("{0} {1}", DateTime.Now.ToLocalTime(), message);
}
}
class ConsoleLogger : ILogger
{
public void WriteLog(string message)
{
Console.WriteLine("{0} {1}",DateTime.Now.ToLocalTime(),message);
}
}
class FileLogger : ILogger
{
private StreamWriter writer;
public FileLogger(string path)
{
writer = File.CreateText(path);
writer.AutoFlush = true;
}
public void WriteLog(string message)
{
writer.WriteLine("{0} {1}", DateTime.Now.ToLocalTime(), message);
}
}
class ClimateMonitor
{
private ILogger logger;
public ClimateMonitor(ILogger logger)
{
this.logger = logger;
}
public void start()
{
while (true)
{
Console.Write("온도를 입력해주세요 : ");
string temperature = Console.ReadLine();
if (temperature == "")
break;
logger.WriteLog("현재온다 : " + temperature);
}
}
}
internal class Program
{
static void Main(string[] args)
{
/*
ILogger logger = new ConsoleLogger();
logger.WriteLog("Hello, World!");
ClimateMonitor monitor = new ClimateMonitor(new FileLogger("Mylog.txt"));
monitor.start();
*/
/*
IFormattableLogger logger = new ConsoleLogger2();
logger.WriteLog("The world is not flat.");
logger.WriteLog("{0} + {1} = {2}",1,1,2);
*/
FlyingCar car = new FlyingCar();
car.Run();
car.Fly();
IRunnable runnable = car as IRunnable;
runnable.Run();
IFlyable flyable = car as IFlyable;
flyable.Fly();
}
}
}
728x90
반응형
댓글