接口的继承

C#中,我们用interface关键字来定义一个接口,为其指定一个名称和一组实例方法签名。Framework类库中几个接口的定义如下:

public   interface  IDisposable
    
{
        
void Dispose();
    }


    
public   interface  IEnumaerable
    
{
        IEnumerator GetEnumerator();
    }


    
public   interface  IEnumerable < T >  : IEnumaerable
    
{
        IEnumerator
<T> GetEnumerator();
    }


    
public   interface  ICollection < T >  : IEnumaerable < T > , IEnumaerable
    
{
        
void Add(T item);
        
void Clear();
        Boolean Contains(T item);
        
void CopyTo(T[] array, Int32 arrayIndex);
        Boolean Remove(T item);
        Int32 Count 
get; }
        Boolean IsReadOnly 
get; }
    }

 一个接口定义可以继承自其他接口。不过,这里只是相当松散地使用继承一词,因为接口继承的工作方式和类继承完全不一样。我个人更愿意把接口继承想象为包含其他接口的约定。 例如,ICollection<T>接口定义便包含了IEnumerable<T>IEnumerable 这两个接口的约定。这意味着:

  • 继承自ICollection<T>接口的任何一个类必须实现ICollection<T>, IEnumerable<T> IEnumerable3个接口定义的所有方法
  • 期望一个对象的类型实现ICollection<T>接口的任何代码都可以假设该对象的类型还实现了IEnumerable<T> IEnumerable接口的方法。

         C#编译器要求将实现了接口的方法标记为publicCLR要求将借口方法标记为virtual。如果不在源代码中将接口方法显示标记为virtual,编译器会把方法标记为virtualsealed,这会阻止派生类重写接口方法。如果将方法显式标记为virtual,编译器会把方法标记为virtual(保留为unsealed),这样一来,派生类就可以重写这个接口方法。

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

namespace  ConsoleApplication1
{
    
class Program
    
{   
        
static void Main(string[] args)
        
       
            Base b 
= new Derived();
            b.Dispose();                            
// Derived
            ((IDisposable)b).Dispose();     //  Derived
        }
    }


    
public interface IDisposable
    
{
        
void Dispose();
    }


    
internal class Base : IDisposable
    
{
        
virtual public void Dispose()
        
{
            Console.WriteLine(
"Base!");
        }

    }


    
internal class Derived : Base
    
{
         
public override void Dispose()
        
{
            Console.WriteLine(
"Derived!");
        }

    }

}

如果接口方法为sealed,派生类就不能重写这个方法。不过,派生类可以重新继承同一个接口,并可以为该接口的方法提供自己的实现,使用”new ”表明该方法重新实现了接口方法

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

namespace  ConsoleApplication1
{    
    
class Program
    
{   
        
static void Main(string[] args)
        
{
            
/******************************/
            Base b 
= new Base();            
            b.Dispose();
            ((IDisposable)b).Dispose();

            
/******************************/
            Derived d 
= new Derived();
            d.Dispose();
            ((IDisposable)d).Dispose();

            
/******************************/
            b 
= new Derived();
            b.Dispose();
            ((IDisposable)b).Dispose();
        }

    }


    
public interface IDisposable
    
{
        
void Dispose();
    }


    
internal class Base : IDisposable
    
{
        
//Dispose方法被隐式标记为sealed, 不能被重写
        public void Dispose()
        
{
            Console.WriteLine(
"Base!");
        }

    }


    
internal class Derived : Base, IDisposable
    
{
        
//这个方法不能重写Base的Dispose方法
        
//'new'用于表明该方法重新实现了IDisposable的Dispose方法
        new public void Dispose()
        
{
            Console.WriteLine(
"Derived!");
        }

    }

}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值