惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

B
Blog RSS Feed
K
Kaspersky official blog
Forbes - Security
Forbes - Security
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
P
Proofpoint News Feed
G
GRAHAM CLULEY
V
Vulnerabilities – Threatpost
Security Latest
Security Latest
Scott Helme
Scott Helme
S
Securelist
美团技术团队
T
Threat Research - Cisco Blogs
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
S
SegmentFault 最新的问题
W
WeLiveSecurity
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Apple Machine Learning Research
Apple Machine Learning Research
The Cloudflare Blog
AI
AI
L
Lohrmann on Cybersecurity
S
Security Affairs
Cloudbric
Cloudbric
SecWiki News
SecWiki News
爱范儿
爱范儿
雷峰网
雷峰网
Engineering at Meta
Engineering at Meta
C
Cyber Attacks, Cyber Crime and Cyber Security
大猫的无限游戏
大猫的无限游戏
N
News and Events Feed by Topic
I
InfoQ
S
Secure Thoughts
AWS News Blog
AWS News Blog
A
About on SuperTechFans
Schneier on Security
Schneier on Security
酷 壳 – CoolShell
酷 壳 – CoolShell
The Last Watchdog
The Last Watchdog
D
Darknet – Hacking Tools, Hacker News & Cyber Security
C
Check Point Blog
P
Palo Alto Networks Blog
博客园 - 【当耐特】
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Google DeepMind News
Google DeepMind News
Latest news
Latest news
I
Intezer
博客园_首页
C
CXSECURITY Database RSS Feed - CXSecurity.com
V
V2EX
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
L
LangChain Blog
D
Docker

博客园 - 言午

Nopcommerce 二次开发2 Admin Nopcommerce 二次开发1 基础 Nopcommerce 二次开发2 WEB Nopcommerce 二次开发0 sqlce中不支持sp_rename修改表名 C#读取Excel遇到无法读取的解决方法 狼奔代码生成器 银行账户类 累 interface 抽象类 抽象方法 Message 类的继承 事件event 委托 代理 索引! 第五周作业 第四周作业 考试! 线程安全 二 线程安全 一
多态(虚方法)
言午 · 2012-04-11 · via 博客园 - 言午

使用虚方法实现“多态”

背景:

    

class Animal
    {
        public string name; public int age; public float temperature;
        public  void Eat()
        {
            Console.WriteLine("Animal 吃东西");
        }
    }
    class Wolf : Animal
    {
        public string color;
        public void hunt(){}
        public  void Eat()
        {
            Console.WriteLine("Wolf 吃 羊");
        }
    }
    class Fish:Animal
    {       
        public void Swim() { }
        public void Eat()
        {
            Console.WriteLine("Fish 吃东西 小Fish");
        }
    }   

在main方法中使用这段代码

            Wolf hui = new Wolf();
            hui.Eat();
           
            Fish jin = new Fish();
            jin.Eat();

发现,使用的Eat方法都是派生类的。

但是,如果这样使用

            Animal a;
            a = hui;           
            a.Eat();   

            a = jin;            
            a.Eat();

使用的Eat方法,却是基类的!!!

这显然不是我们想要的。

我们需要,根据对象运行时,所属类的不同,调用相应类的方法。

这就是多态

关键词 virtual  override。在基类中的方法,加上修饰符virtual ,在派生类中加上override 。代码如下:

    

class Animal
    {
        public string name; public int age; public float temperature;
        public virtual  void Eat()
        {
            Console.WriteLine("Animal 吃东西");
        }
    }
    class Wolf : Animal
    {
        public string color;
        public void hunt(){}
        public override void Eat()
        {
            Console.WriteLine("Wolf 吃 羊");
        }
    }
    class Fish:Animal
    {       
        public void Swim() { }
        public override void Eat()
        {
            Console.WriteLine("Fish 吃东西 小Fish");
        }
    }   

再看看程序运行的结果