代码之家  ›  专栏  ›  技术社区  ›  mark vanzuela

财产继承问题

  •  2
  • mark vanzuela  · 技术社区  · 15 年前

    如何从接口继承c中的属性并在类上给该属性其他名称?

    例如:

    public interface IFoo
    {
      int Num {get;set;}
    }
    
    public class IFooCls : IFoo
    {
      int Ifoo.Num{get;set}
    }
    

    在这种情况下,接口中的属性名在类中也是相同的。我想给出类上的其他属性名,但在本例中仍然指向接口中的“num”。在VB中,我们可以这样做:

    Public ReadOnly Property UserId() As String Implements System.Security.Principal.IIdentity.Name
        Get
            Return _userId
        End Get
    End Property
    
    2 回复  |  直到 15 年前
        1
  •  0
  •   Oded    15 年前

    如果继承一个属性,则继承它、名称、类型和所有内容。无法更改名称。

    如果需要,可以使用其他名称编写另一个属性,并从中调用继承的属性(仍然必须实现原始属性)。

    看看你 可以 做对 this MSDN页。

    实现你想要的唯一方法 UserId 财产 影响因素 IIdentity )是呼叫 IIdentity.Name 从它)。

    这将隐藏 身份识别 属性来自类的用户(除非他们强制转换为 身份认同 ):

    public class myIdentity : IIdentity
    {
       public string IIdentity.Name {get; set;}
    
       public string UserId 
       { 
          get 
          { 
            return IIdentity.Name;
          }
    
          set 
          { 
            IIdentity.Name = value;
          }
    }
    
        2
  •  1
  •   Igor Zevaka    15 年前

    这里有一种方法来模拟你在C语言中要做的事情#

    public interface Foo {
      string Name {get;set;}
    }
    
    pubilc class Bar : Foo {
    
    #region Foo implementation
      public string Name {get{return UserName;} set{UserName = value;}}
    #endregion //Foo implementation
    
      public string UserName {get; set;}
    }