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

推荐订阅源

U
Unit 42
T
Threatpost
C
CERT Recently Published Vulnerability Notes
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Security Archives - TechRepublic
Security Archives - TechRepublic
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
K
Kaspersky official blog
Application and Cybersecurity Blog
Application and Cybersecurity Blog
Attack and Defense Labs
Attack and Defense Labs
N
News and Events Feed by Topic
Project Zero
Project Zero
H
Heimdal Security Blog
C
Cybersecurity and Infrastructure Security Agency CISA
Know Your Adversary
Know Your Adversary
Google Online Security Blog
Google Online Security Blog
W
WeLiveSecurity
D
Darknet – Hacking Tools, Hacker News & Cyber Security
Schneier on Security
Schneier on Security
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
N
News | PayPal Newsroom
Hacker News - Newest:
Hacker News - Newest: "LLM"
H
Hacker News: Front Page
L
LINUX DO - 热门话题
Spread Privacy
Spread Privacy
T
Threat Research - Cisco Blogs
Cloudbric
Cloudbric
V
Vulnerabilities – Threatpost
Hacker News: Ask HN
Hacker News: Ask HN
S
Securelist
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
TaoSecurity Blog
TaoSecurity Blog
NISL@THU
NISL@THU
N
News and Events Feed by Topic
S
Security Affairs
The Last Watchdog
The Last Watchdog
T
Tor Project blog
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
T
The Exploit Database - CXSecurity.com
Simon Willison's Weblog
Simon Willison's Weblog
P
Palo Alto Networks Blog
AWS News Blog
AWS News Blog
P
Proofpoint News Feed
C
Cisco Blogs
C
Cyber Attacks, Cyber Crime and Cyber Security
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
L
LINUX DO - 最新话题
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
T
Tenable Blog
C
CXSECURITY Database RSS Feed - CXSecurity.com
S
Schneier on Security

博客园 - 为森

NPM设置镜像 WCF 自托管、无配置文件实现jsonp(跨域)的访问 关于nodejs4.0 npm乱码以及离线全局安装时要注意的问题 C# 正则表达式 转自-每日一bo 关于async & await(TAP)异步模型的异常捕获 解决Bootstrap 附加导航(Affix)的问题和使用时若干注意事项 一次小异常的排查,悲剧的无以复加!!! async & await 小扩展大用处,自己扩展一个ForeachRead吧 转 常用工具和技术 implicit和 explicit关键字 MVC 自定义 以表达式树为参数的htmlhelper 解决解密时出现"要解密的数据的长度无效" 或 "填充无效无法被移除" 的错误 EF 的一些不常用的功能 详细说明 配置 Sublime Text 开发node.js(windows)包括sub2和sub3的区别 sql 判断 数据库 表 字段 是否存在 转 WCF中同步和异步通讯总结 关于WCF 解决EF一对一或多对一的删除
一个Public的字段引起的,谈谈继承中的new
为森 · 2015-01-28 · via 博客园 - 为森

一直觉得对c#面向对象这块已经掌握的很好了,因为正常情况下字段一般我们设计成私有的,今天突然想到一个实验,如下有两个很简单的类:

public class Farther
    {
        public int a = 100;
        public virtual int A
        {
            get
            {
                return a;
            }
        }
    }

    public class Son : Farther
    {
        public new int a = 200;
        public override int A
        {
            get
            {
                return a;
            }
        }
    }

注意字段是public的

那么对于下面的输出你能写出正确答案吗?

       Son obj = new Son();
            var obja = obj as Farther;
            Farther objb = obj;
            var objc = (Farther)obj;

            Console.WriteLine(obj.a);
            Console.WriteLine(obja.a);
            Console.WriteLine(objb.a);
            Console.WriteLine(objc.a);

            Console.WriteLine(obj.A);
            Console.WriteLine(obja.A);
            Console.WriteLine(objb.A);
            Console.WriteLine(objc.A);

            Console.ReadKey();

如果我将Son中的override注释掉那答案是什么?

public class Son : Farther
    {
        public new int a = 200;
        //public override int A
        //{
        //    get
        //    {
        //        return a;
        //    }
        //}
    }

下面贴答案

未注释:

已注释:

结论:无论是字段或是方法或是属性,加上new 都会在显示的通过父类访问时表现为父类的特征,这里方法和属性我们很常用,但是字段我们可以认为默认是sealed的,即使在子类中不加new也是new一个新的(会弹出一个警告),所以微软在设计的时候是非常统一的。在这里也回应有些人认为在继承中父类的字段会完全被覆盖而消失掉的问题。

本文来自博#客#园