Use polymorphism in objects of classes that implement the same interface?

1

Is it possible? Or they must necessarily inherit from the same parent class ...

public interface interfaz
{

}

public class a : interfaz
{

}

public class b : interfaz
{

}

public class prueba
{
    public void X()
    {
        a objetoA = new b();
    }
}

The code such that it gives a compilation error since classes a and b are obviously of different types.

Real example;

public partial class ConfigNivel : UserControl
{
    public int GetTotal(){};
}

public partial class ConfigCPU : UserControl
{
    public int GetTotal(){};
}

These are views that I load in a StackPanel and I would like to always call their method GetTotal() once they have instantiated them, regardless of the class they are.

NEW EDIT

The views that you load in the StackPanel can be 3, 4, 5, 6, 7 ... whatever they are, they all inherit from the UserControl class, and all of them have a method called GetTotal() , which returns the calculation of a sum of properties of that object. What I want to do is that they are of the class that are able to call their GetTotal () method within a For or a Foreach and get the sum of all. The problem I have now is that they are objects of different classes: ConfigNivel , ConfigCPU etc ... and I can not use poliformfismo. As they are inheriting from UserControl and C # does not support inheriting of several classes (or so I think) I do not know how to do it ... I hope you have explained me well.

    
asked by Edulon 08.02.2018 в 19:12
source

2 answers

2

As you say, you can not initialize a class of type a with an instance of type b . What you should do in these cases is to use the interface as a type. For example, like this:

interfaz objetoA = new a();
interfaz objetoB = new b();

If you then need to check what type each of the created instances is, you can use is or typeof :

if (objetoA is A) { ... }
if (objetoA.GetType() == typeof(B)) { ... }

Finally, if you need to assign this variable to a specific type, you should make a cast:

A objetoC = (A)objetoA;

Edit:

Let's use your real example:

public interface interfaz
{
    int GetTotal();
}

public class a : interfaz
{
    public int GetTotal() { return 1; }
}

public class b : interfaz
{
    public int GetTotal() { return 2; }
}

Instantiating and calling the interface method has no problem:

a varA = new a();
b varB = new b();

varA.GetTotal();
varB.GetTotal();

We create a method that receives an object of a class that implements interfaz :

public static int metodo(interfaz variable)
{
    return variable.GetTotal();
}

Call this method either:

metodo(varA);
metodo(varB);
    
answered by 09.02.2018 / 09:06
source
2

I do not know if I understand your question well, I think your problem is that you do not have the signature of the method in the interface.

An example:

We define the method in the interface, in this way all the classes that implement the interface must have this method:

interface InterfazComun
{
    int GetTotal();
}

We define classes A and B, both inherit from the interface. ClassA has an additional method, which is specific to the class, not the interface, therefore ClassB is not required to implement it.

 public class ClaseA: InterfazComun
    {
        public char letra;
        public ClaseA()
        {
            letra = 'A';
        }
        public int GetTotal()
        {
            return 1;
        }

        public string GetNombre()
        {
            return "Este es el metodo A";
        }
    }

public class ClaseB: InterfazComun
    {
        public char letra;
        public ClaseB()
        {
            letra = 'B';
        }
        public int GetTotal()
        {
            return 2;
        }
    }

And a console program that shows different ways to access the GetTotal () method:

class Program
    {
        public static string DevolverResultadoLetra(InterfazComun interfazComun)
        {
            return "Este es el resultado de la clase pasada como parámetro: " + interfazComun.GetTotal().ToString();
        }
        static void Main(string[] args)
        {
            ClaseA claseA = new ClaseA();
            ClaseB claseB = new ClaseB();

            Console.WriteLine("Este es el resultado de la clase A "+claseA.GetTotal().ToString());
            Console.WriteLine("Este es el resultado de la clase B " + claseB.GetTotal().ToString());

            Console.WriteLine(DevolverResultadoLetra(claseA));
            Console.WriteLine(DevolverResultadoLetra(claseB));

            Console.ReadKey();
        }


    }

I hope this example helps you. If you do not mean this, please specify a little more about your problem.

    
answered by 09.02.2018 в 19:44