显而易见,将我这段代码跑一遍就可以理解并会用了
public interface IAnimal
{
void Move();
}
public class Animal : IAnimal
{
public string Name { get; set; }
public int Age { get; set; }
public Animal(string name, int age)
{
Name = name;
Age = age;
}
public virtual void MakeSound()
{
Console.WriteLine("The animal makes a sound.");
}
public void Eat(string food)
{
Console.WriteLine("{0} is eating {1}.", Name, food);
}
public void Sleep()
{
Console.WriteLine("{0} is sleeping.", Name);
}
public void Move()
{
Console.WriteLine("{0} is moving.", Name);
}
}
public class Dog : Animal
{
public string Breed { get; set; }
public Dog(string name, int age, string breed) : base(name, age)
{
Breed = breed;
}
public override void MakeSound()
{
base.MakeSound();
Console.WriteLine("The dog barks.");
}
public void PrintInfo()
{
Console.WriteLine("Name: {0}", base.Name);
Console.WriteLine("Age: {0}", base.Age);
Console.WriteLine("Breed: {0}", Breed);
}
public void PlayFetch()
{
Console.WriteLine("{0} is playing fetch.", Name);
}
}
public class Program
{
static void Main(string[] args)
{
Dog myDog = new Dog("Buddy", 3, "Golden Retriever");
myDog.MakeSound();
myDog.PrintInfo();
myDog.Eat("dog food");
myDog.Sleep();
myDog.Move();
myDog.PlayFetch();
IAnimal myAnimal = myDog;
myAnimal.Move();
}
}