【问题标题】:How can i call method from class but this method implemented from any interface?我如何从类中调用方法,但该方法是从任何接口实现的?
【发布时间】:2011-02-25 18:10:54
【问题描述】:

我尝试调用 base.Alan();在哈钦布尔。但是基础。不要给智能感知艾伦方法

   public double HacimBul()
        {
            throw new Exception();
            //return base..... --> how can i see base.Alan();
        }

namespace interfaceClass
{
    class Program
    {
        static void Main(string[] args)
        {

        }
    }

    interface Ikenar
    {
       double kenar { get; set; }
    }

    interface Iyukseklik
    {
        double yuksekli {get; set;}
    }
    interface IAlan
    {
        double Alan();
    }
    interface IHacim
    {
        double Hacim();
    }

    class Alan : Ikenar, IAlan
    {
        public double kenar
        {
            get;
            set;
        }

        double IAlan.Alan()
        {
            return kenar * kenar;
        }
    }

    class Hacim : Alan, Iyukseklik
    {

        public double kenar
        {
            get;
            set;
        }

        public double yuksekli
        {
            get;
            set;
        }

        public double HacimBul()
        {
            throw new Exception();
            //return base..... --> how can i see base.Alan();
        }
   }
}

【问题讨论】:

  • 我不明白你的问题是什么。
  • 顺便说一句。为什么不接受答案?

标签: c# .net visual-studio oop


【解决方案1】:
double IAlan.Alan() // this is explicit interface implementation
{
   return kenar * kenar;
}

通过在方法名称前加上接口,您是在告诉用户方法Alan 只能通过接口引用而不是类的引用来使用。

把上面改成

double Alan()
{
   return kenar * kenar;
}

Alan 类中。

【讨论】:

  • 这不起作用,因为该类现在是私有的,因此派生类无法访问
  • 好吧,如果它是公共的而不是私有的,它会起作用......但他还需要重命名该类,因为它与方法具有相同的名称。无论如何,它还是值得重命名的......
  • @my self 应该说该方法现在是私有的
【解决方案2】:

不,目前无法使用。由于显式接口实现,基本实现可用于IAlan 类型的表达式。

你可以使用这个:

return ((IAlan)this).Alan();

或者您可以在 Alan 中使用隐式接口实现,尽管这意味着重命名类或方法。

对于其他试图解决这个问题的人来说,它更简单地表达为这样 - 以一种不将方法名作为类名重用的方式:

public interface IFoo
{
    void Bar();
}

public class BaseFoo : IFoo
{
    void IFoo.Bar()
    {
        // Do something
    }
}

public class DerivedFoo : BaseFoo
{
    void OtherMethod()
    {
        // Doesn't compile due to explicit interface implementation
        base.Bar();

        // Will work
        ((IFoo)this).Bar();
    }
}

【讨论】:

    【解决方案3】:

    你已经明确实现了 Alan,所以你必须选择

    要么更改方法的名称,要么更改名为 Alan 的类,这样您就不会发生名称冲突并将该方法实现为普通方法,例如

    public double Alan() //name of class can't be Alan in this case
    {
       return kenar * kenar;
    }
    

    public double HacimBul()
     {
        return ((IAlan)this).Alan();
     }
    

    您没有获得智能感知的原因是该方法仅在 Alan 类型的对象被声明为 IAlan 时才可用,否则 Alan 方法将被隐藏

    【讨论】:

      【解决方案4】:

      该方法不是公共的或受保护的,它是您定义它的私有方式,甚至比private 更私有,只有通过接口才能使用。

      这意味着为了调用该方法,您必须:

      • 将 Alan 方法的显式实现更改为 Alan 中的隐式实现(使方法公开):

        public void Alan()
        {
        }
        
      • 或者,您必须在调用期间强制转换实例:

        ((IAlan)this).Alan();
        

      不幸的是,使用最后的语法,您实际上不会调用“base.Alan”,而只是调用“this.Alan”,如果您还在后代类中覆盖该方法,这可能会有所不同。

      这是一个例子:

      using System;
      
      namespace interfaceClass
      {
          public interface ITest
          {
              void Execute();
          }
      
          public class Base : ITest
          {
              void ITest.Execute()
              {
                  Console.Out.WriteLine("Base.ITest.Execute");
              }
          }
      
          public class Descendant : Base, ITest
          {
              void ITest.Execute()
              {
                  Console.Out.WriteLine("Descendant.ITest.Execute");
              }
      
              public void Test()
              {
                  // There's no way to call "base.Execute()" here
                  ((ITest)this).Execute();
              }
          }
      
          class Program
          {
              static void Main(string[] args)
              {
                  Descendant d = new Descendant();
                  d.Test();
              }
          }
      }
      

      【讨论】:

      • 感谢 Lasse V. Karlsen。我怎么认识你 : 我的 msn : yusufkaratoprak@yahoo.com 请加我:)
      • ((IAlan)this).Alan 中基类的潜在覆盖方法的问题可以通过 ((IAlan)base).Alan 来避免 :)
      • 不,如果您尝试“使用关键字 'base' 在此上下文中无效”,则会收到此编译器错误。您确实在发布之前尝试过您的建议吗?
      • @Phsika,我的 MSN 地址已发布在我的个人资料中,但我建议您继续在 Stack Overflow 上发布问题,除非您对此特定答​​案有疑问并且该问题与特定解决方案有关,并且因此其他人都不会感兴趣,但在这种情况下,我建议您使用我的电子邮件地址(与 MSN 地址相同,仍发布在我的个人资料中。)
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-03-13
      • 2014-11-20
      • 1970-01-01
      • 1970-01-01
      • 2014-04-12
      • 1970-01-01
      • 2020-02-20
      相关资源
      最近更新 更多