How can I access an interface method implemented in a derived class from a child class?

I have two interfaces A, B both have the same method declarations. I have a class C inheriting from interfaces A, B. I have another class D inheriting from C. Now I want to access the implemented methods in C from D

interface A
{
    int add(int x, int y);
    int mul(int x, int y);
}

interface B
{
    int add(int x, int y);
    int mul(int x, int y);
}

public class C : A,B
{
    int A.add(int x,int y)
    {
        return x + y;
    }

    int A.mul(int x,int y)
    {
        return 0;
    }

    int B.add(int x, int y)
    {
        return x;
    }

    int B.mul(int x, int y)
    {
        return y;
    }  
}


class D : C
{
}

      

How do I access methods in C from D?

+3


source to share


5 answers


How do I access methods in C from D?

You must use a link with the compile time of the corresponding interface. For example:

class D
{
    public void FooA()
    {
        A a = this;
        Console.WriteLine(a.mul(...));
    }

    public void FooB()
    {
        B b = this;
        Console.WriteLine(b.mul(...));
    }
}

      

Of course, you don't need a local variable - you can use:



Console.WriteLine(((A) this).mul(...));

      

... but it gets a little ugly.

This is only because you are using an explicit interface implementation . If you implemented one of the interfaces implicitly, you could just call the methods directly as normal ... but an explicit implementation of an interface only allows the user to call through that interface.

+7


source


Explicitly calling an interface method should always work



((A)this).mul(1,1);

      

+3


source


You can use this code because you need to specify the interface from which you want to use the method (A or B):

((A)this).add(1, 1);

      

+1


source


As others have said, casting is, of course, one way to do this. It's quick and easy, but if you're going to use it, it's very annoying. The output in this case is properties that provide access to the members exposed by the interface, and which conveniently group them together, making them easier to use:

Easy access without additional casting (you only do this once inside getters properties - see below class C

):

class Program
{
    static void Main(string[] args)
    {
        C c = new C();
        c.As.add(1, 2);
    }
}

      

Interfaces:

public interface A
{
    int add(int x, int y);
    int mul(int x, int y);
}

public interface B
{
    int add(int x, int y);
    int mul(int x, int y);
}

      

C

-class:

public class C : A, B
{
    // Methods from the A-interface.
    public A As { get { return (A)this; } }

    // Methods from the B-interface.
    public B Bs { get { return (B)this; } }

    int A.add(int x, int y)
    {
        return x + y;
    }
    int A.mul(int x, int y)
    {
        return 0;
    }
    int B.add(int x, int y)
    {
        return x;
    }
    int B.mul(int x, int y)
    {
        return y;
    }
}

      

D

-class:

public class D : C
{
    public D()
    {
        base.As.add(1, 2);
        base.Bs.add(3, 4);
    }
}

      

+1


source


You can check it out,

 using System;

 public class Program
{
    public static void Main()
     {

     D ds=new D(10,12);

     int valueAddtion=((A)ds).add(20,122);

     int valueMultiplication=((B)ds).mul(20,11);
     Console.WriteLine("Mainapplicatin Value  of A= " +valueAddtion+" multiplication value=  "+valueMultiplication);

    }
}

      

// your code segment is here

 class D : C
 {
  public D()
  {
 int valueAdd=((A)this).add(10,11);
 int valueMul=((B)this).mul(20,11); 
 Console.WriteLine("Addition Value  of A= " +valueAdd+" multiplication value= "+valueMul);
  }

public D(int x,int y):this()
{
 int valueAdd=((A)this).add(x,y);
 int valueMul=((B)this).mul(x,y);   
 Console.WriteLine("Paremeterized Value  of A= " +valueAdd+" multiplication value= "+valueMul);
}
}

      

There will be a way out

The value of adding the multiplication value A = 21 = 11

Parameterized value A = 22 multiplication value = 12

Mainapplicatin value A value = 142 multiplication = 11

0


source







All Articles