using System;

public class Animal
{
    public string Name { get; set; }

    public virtual void Speak()
    {
        Console.WriteLine("I am an animal.");
    }
}

public class Cat : Animal
{
    public override void Speak()
    {
        Console.WriteLine("Meow!");
    }
}

public class Dog : Animal
{
    public override void Speak()
    {
        Console.WriteLine("Woof!");
    }
}

class Program
{
    static void Main(string[] args)
    {
        Animal animal = new Animal();
        animal.Speak();

        Cat cat = new Cat();
        cat.Speak();

        Dog dog = new Dog();
        dog.Speak();

        Console.ReadKey();
    }
}
写一个C#猫狗继承关系

原文地址: https://www.cveoy.top/t/topic/Emk 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录