using System; class Program{ static void Main(string[] args){ var runner = new Runner(); runner.Run(); } } class Runner{ private Logger _logger; public Runner(){ _logger = new Logger(); } public void Run(){ // Do some work _logger.Log("Message to be logged"); } } class Logger{ public void Log(string message){ Console.WriteLine(message); } }
using System; class Program{ static void Main(string[] args){ var logger = new Logger(); var runner = new Runner(logger); runner.Run(); } } class Runner{ private ILogger _logger; public Runner(ILogger logger){ _logger = logger; } public void Run(){ // Do some work _logger.Log("Message to be logged"); } } interface ILogger{ void Log(string message); } class Logger : ILogger{ public void Log(string message){ Console.WriteLine(message); } }
Message to be logged
以上がC# での依存性注入の説明の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。