• 认证题 211


       实现多态

    1.Animal.cs(类文件)

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;

    namespace ConsoleApplication1
    {
    class Animal
    {
    private bool m_sex;
    private string m_sound;

    public bool Sex
    {
    get
    {
    return m_sex;
    }

    set
    {
    m_sex = value;
    }
    }

    public string Sound
    {
    get
    {
    return m_sound;
    }

    set
    {
    m_sound = value;
    }
    }

    public Animal()
    {
    m_sex = false;
    Sound = "Howl...";
    }
    public virtual string Rore()
    {
    return Sound;
    }
    }
    class Dog : Animal
    {
    public Dog()
    {
    Sex = true;
    Sound = "Wow...";
    }
    public override string Rore()
    {

    return "Dog:" + Sound;
    }
    }
    class Cat : Animal
    {
    public Cat()
    {
    Sound = "Miaow...";
    }
    public override string Rore()
    {

    return "Cat:" + Sound;
    }
    }
    class Cow : Animal
    {
    public Cow()
    {
    Sound = "Moo...";
    }
    public override string Rore()
    {

    return "Cow:" + Sound;
    }
    }

    }

    2.实例化

    static void Main(string[] args)
    {
    string s1,s2,s3;
    Animal animal = new Cow();
    s1=animal.Rore();
    Console.WriteLine(s1);
    Animal dog = new Dog();
    s2=dog.Rore();
    Console.WriteLine(s2);
    Animal cat = new Cat();
    s3=cat.Rore();
    Console.WriteLine(s3);
    }

    3.结果

  • 相关阅读:
    i春秋CTF-web-upload
    [转]SEP 11.x 迁移
    通过SEP禁用USB
    通过SEP屏蔽共享文件夹
    SEP图示
    离线更新SEPM服务器的病毒定义库
    SEPM安装完之后的一些细节之处
    Bloomberg SEP 12.x 迁移小记
    LiveUpdate Adminstrator配置手册
    Symantec更新服务器
  • 原文地址:https://www.cnblogs.com/slpa/p/7534995.html
Copyright © 2020-2023  润新知