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

推荐订阅源

Attack and Defense Labs
Attack and Defense Labs
The GitHub Blog
The GitHub Blog
C
Check Point Blog
博客园_首页
MongoDB | Blog
MongoDB | Blog
N
Netflix TechBlog - Medium
F
Full Disclosure
Microsoft Security Blog
Microsoft Security Blog
爱范儿
爱范儿
Recent Announcements
Recent Announcements
阮一峰的网络日志
阮一峰的网络日志
G
GRAHAM CLULEY
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
T
Threat Research - Cisco Blogs
C
Cybersecurity and Infrastructure Security Agency CISA
V
Vulnerabilities – Threatpost
K
Kaspersky official blog
博客园 - 司徒正美
S
Schneier on Security
T
The Exploit Database - CXSecurity.com
Project Zero
Project Zero
云风的 BLOG
云风的 BLOG
Cisco Talos Blog
Cisco Talos Blog
Know Your Adversary
Know Your Adversary
雷峰网
雷峰网
V
V2EX - 技术
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Spread Privacy
Spread Privacy
罗磊的独立博客
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
S
Security Affairs
SecWiki News
SecWiki News
Schneier on Security
Schneier on Security
O
OpenAI News
Jina AI
Jina AI
PCI Perspectives
PCI Perspectives
Cyberwarzone
Cyberwarzone
Y
Y Combinator Blog
Apple Machine Learning Research
Apple Machine Learning Research
B
Blog RSS Feed
I
InfoQ
D
Docker
P
Palo Alto Networks Blog
Recorded Future
Recorded Future
M
MIT News - Artificial intelligence
博客园 - Franky
B
Blog
Scott Helme
Scott Helme
博客园 - 叶小钗
D
DataBreaches.Net

博客园 - Freeman Shen

.NET面试题 Check Constraints限制本表的行数 数据库中主键和外键的设计原则 讲解SQL Injection一篇不错的文章,地址贴一下 Queue<(Of <(T>)>) 类,msdn上的一篇文章,便于查看 初次接触.Net下的信号量(semaphore) 某“高人”谈论股市,对现在行情的分析 编辑电影字幕,编辑srt格式 左联,右联和内联的区别(图示) 用JavaScript如何获取客户端的操作系统是window2000还是window xp? 在IBatisNet框架,MasterPage,基类中配置Ajaxpro 自从离开哈尔滨后就没再管理这个blog了,以后还得继续努力啊 70-528试题2:仍然是有关xml的 题1的答案是(B),这是一个xml和schema的问题! 70-582试题1 由清明节想到的 今天心情不太好,没什么可写的 - Freeman Shen 由DataBind()而发现的 - Freeman Shen 生命之价
(转)C#中的Abstract和Virtual函数区分,因我老弄不明白这个问题,所以转到这儿
Freeman Shen · 2009-08-19 · via 博客园 - Freeman Shen

    发现很多朋友对于C#中的Abstract和Virtual函数区分得不是很清楚,下面我通过两段代码让大家看看这两者之间到底有什么区别~~

开发环境: VS.net 2005

使用方法: 用以下代码覆盖program.cs中的代码,然后run即可

第一段代码: Abstract的用法

using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication3
{
    public abstract class Book
    {
        //抽象方法,不含主体,抽象方法所在类必须为抽象类,派生类必须实现该方法
        public abstract void Introduce();
    }

    public interface iBook
    {
    }

    public class JavaBook : Book
    {
        //实现抽象方法,必须实现,必须添加override关键字
        public override void Introduce()
        {
            Console.WriteLine("I'm Java");
        }

//            // Compile Error
//            public void Introduce()
//            {
//                Console.WriteLine("I'm Java");
//            }
    }


    public class test
    {
        public test()
        {
            JavaBook javaBook = new JavaBook();
            javaBook.Introduce();      //将调用JavaBook中Introduce()
            Book book = new JavaBook();
            book.Introduce();      //将调用JavaBook中Introduce()
        }
        public static void Main()
        {
            test t = new test();
        }
    }
}

第二段代码: Virtual的用法以及override的用法

using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication2
{
    public abstract class Book
    {
        public virtual void Introduce()
        {
            Console.WriteLine("I'm book");
        }

        public virtual void SayHi()
        {
            Console.WriteLine("Hi, I'm book");
        }
    }


    public class JavaBook : Book
    {
        public override void Introduce()
        {
            Console.WriteLine("I'm Java");
        }

        //注意,此方法没有override父类的方法
        public void SayHi()
        {
            Console.WriteLine("Hi, I'm Java");
        }
    }


    public class test
    {
        public test()
        {
            JavaBook javaBook = new JavaBook();
            Book book = new JavaBook();
            javaBook.Introduce();       //将调用JavaBook中Introduce()
            book.Introduce();       //将调用JavaBook中Introduce()

            javaBook.SayHi();      //将调用JavaBook中SayHi()
            book.SayHi();           //将调用Book中SayHi()
        }

        public static void Main()
        {
            test t = new test();
        }
    }
}

第三段代码: new的用法

using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication4
{
    public abstract class Book
    {
        public void Introduce()
        {
            Console.WriteLine("I'm book");
        }

        public void SayHi()
        {
            Console.WriteLine("Hi, I'm book");
        }
    }


    public class JavaBook : Book
    {
        // 没有加new,但C#默认行为会加上new
        public void Introduce()
        {
            Console.WriteLine("I'm Java");
        }

        // 显式加上new,跟不加new实际效果一样,只是加new之后会消除compile warning
        public new void SayHi()
        {
            Console.WriteLine("Hi, I'm Java");
        }
    }


    public class test
    {
        public test()
        {
            JavaBook javaBook = new JavaBook();
            Book book = new JavaBook();

            javaBook.Introduce();       //将调用JavaBook中Introduce()
            javaBook.SayHi();           //将调用JavaBook中SayHi()
           
            book.Introduce();       //将调用Book中Introduce()
            book.SayHi();           //将调用Book中SayHi()
        }
        public static void Main()
        {
            test t = new test();
        }
    }
}