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

推荐订阅源

Google DeepMind News
Google DeepMind News
I
InfoQ
Engineering at Meta
Engineering at Meta
D
DataBreaches.Net
L
LangChain Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Recent Announcements
Recent Announcements
GbyAI
GbyAI
爱范儿
爱范儿
Microsoft Security Blog
Microsoft Security Blog
腾讯CDC
美团技术团队
罗磊的独立博客
Microsoft Azure Blog
Microsoft Azure Blog
WordPress大学
WordPress大学
T
The Blog of Author Tim Ferriss
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
雷峰网
雷峰网
M
MIT News - Artificial intelligence
D
Docker
MongoDB | Blog
MongoDB | Blog
F
Fortinet All Blogs
博客园 - 叶小钗

博客园 - bartholomew

关键词:应用程序扩展,通配符应用程序映射 Silverlight 2 Beta 1在Firefox下显示时的一点小问题~ Visual Studio也有调试禁区?! 关于SQL Server 2005的版本号 使用动态SQL的一点小技巧 通过本地IIS SMTP服务器发送邮件时提示“邮箱不可用”的解决办法 使用System.Net.Mail.SmtpClient发送邮件时出现的乱码问题 写代码的心情 在XP上安装SQL Server 2000、Visual studio .net 2003、Visual studio 2005、SQL Server 2005…… XPS M1210到了~~~~ 在dell的网上订购了XPS M1210,耐心等待中…… 对PropertyGrid控件中PropertyValueChanged事件的探讨 关于邮件群发 关于Dotnet中的线程池 Dotnet中强行关闭多线程应用程序的所有线程 工作之余,自省~ 创建某控件的线程之外的其他线程试图调用该控件引发的问题 古怪的ConfigurationManager类 多线程编程中Join与WaitOne的区别
new作为修饰符时的使用,以及接口的显式实现
bartholomew · 2008-06-26 · via 博客园 - bartholomew

先上代码:

namespace ConsoleApplication1
{
    
class Program
    
{
        
static void Main(string[] args)
        
{
            BClass b 
= new BClass();

            ((AClass)b).Output();
            b.Output();
            ((IA)b).Output();

            Console.ReadLine();
        }

    }


    
public interface IA
    
{
        
void Output();
    }


    
public abstract class AClass : IA 
    
{
        
public AClass()
        
{
            Console.WriteLine(
"AClass");
        }


        
public virtual void Output()
        
{
            Console.WriteLine(
"AClass Output");
        }


        
IA Members
    }


    
public class BClass : AClass
    
{
        
public BClass()
        
{
            Console.WriteLine(
"BClass");
        }


        
public new void Output()
        
{
            Console.WriteLine(
"BClass Output");
        }

    }

}

输出为:
AClass...
BClass...
AClass Output...
BClass Output...
AClass Output Exlicitly...

将代码中的new 替换为override,则输出变为:
AClass...
BClass...
BClass Output...
BClass Output...
AClass Output Exlicitly...

从以上代码我们可以看出:
new 作为修饰符时,其作用是隐藏从基类继承过来的方法,注意,仅仅是隐藏,这就意味着我们还是有办法调用到被隐藏的那个方法。而要调用被隐藏的那个方法,实现办法和要调用显示实现的接口方法一样。
而如果用override时,就会重写从基类继承过来的方法,那么,就怎么也调用不到基类的那个方法了。