簡體   English   中英

如何從類中調用方法,但如何從任何接口實現此方法?

[英]How can i call method from class but this method implemented from any interface?

我試圖打電話給base.Alan(); 在HacimBul。 但是基礎。 不要給出智能的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();
        }
   }
}

不,目前暫時無法使用。 由於顯式接口實現,因此基本實現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();
    }
}

該方法不是公共的或受保護的,它是您定義方法的私有方法,甚至比私有方法還要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();
        }
    }
}
double IAlan.Alan() // this is explicit interface implementation
{
   return kenar * kenar;
}

通過在方法名稱前加上接口前綴,您就是在告訴用戶方法Alan僅可通過接口而不是類的引用來使用。

將以上更改為

double Alan()
{
   return kenar * kenar;
}

Alan班里面。

您已經對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方法將被隱藏

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM