zoukankan      html  css  js  c++  java
  • 认证题 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.结果

  • 相关阅读:
    [洛谷P1155] 双栈排序
    [洛谷P4315] 月下”毛景“树
    [洛谷P2486] [SDOI2011]染色
    [HNOI2010] 弾飞绵羊
    mysql注入总结
    cisco交换机实现端口聚合
    python为运维人员打造一个监控脚本
    复习ACCESS注入
    利用sfc文件构建网络渗透
    FTP站点设置
  • 原文地址:https://www.cnblogs.com/slpa/p/7534995.html
Copyright © 2011-2022 走看看