Skip to content

Instantly share code, notes, and snippets.

@Pregum
Created May 9, 2017 14:39
Show Gist options
  • Save Pregum/c10f39c7c9ea03712132796951217bfd to your computer and use it in GitHub Desktop.
Save Pregum/c10f39c7c9ea03712132796951217bfd to your computer and use it in GitHub Desktop.
namespace TemplateMethod
{
public interface Animal
{
string GetVoice();
}
public class Dog : Animal
{
public string GetVoice()
{
return "わん";
}
}
public class Cat : Animal
{
public string GetVoice()
{
return "にゃー";
}
}
}
using System;
using System.Collections.Generic;
namespace TemplateMethod
{
internal class Program
{
private static void Main(string[] args)
{
//ポリモーフィズムのテストコード
var animalList = new List<Animal>
{
new Dog(),
new Cat(),
};
foreach (var animal in animalList)
{
Console.WriteLine(animal.GetVoice());
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment