12
I have a polymorphism problem. But before I explain it, I want to make it clear that I’m still learning a lot, so if you want to throw my structure in the garbage, be my guest. I have a project that converts types of temperatures into others. I have an interface that says each temperature should convert from one type of temperature to another type.
public interface ITemperatura
{
double Convert(double valor, ITemperatura temperatura);
}
Celsius, Farenheit and Kelvin sign Itemperatura. That is, Celsius, for example, would use Convert to convert a temperature to Celsius for another temperature passed in Itemperatura, like Farenheit, for example.
public class Celsius : ITemperatura
{
public double Convert(double valor, ITemperatura temperatura)
{
return 0; // Não deve entrar aqui, esse método só está aqui para respeitar a interface,
// A ideia é fazer o polimorfismo e entrar nos métodos especializados abaixo
}
public double Convert(double valor, Celsius temperatura)
{
return 1; // implementacao de calculo de Celsius para Celsius
}
public double Convert(double valor, Farenheit temperatura)
{
return 2; // implementacao de calculo de Celsius para Farenheit (pois esta classe é celsius e o parametro é farenheit)
}
public double Convert(double valor, Kelvin temperatura)
{
return 3; // implementacao de calcula de Celsius para Kelvin
}
}
Note above that, in addition to the methods of each temperature that are 3, I have one more that receives Itemperatura. It is only there because the interface asks. But it should not be called, the idea is to call the specialized types, Celsius, Farenheit and Kelvin. Note: I did not implement correctly with the specific calculations, only return 1 2 and 3, representing return of Celsius, Farenheit and Kelvin, respectively, beyond the 0 that would be an error return.
I then have the Convert class which has a Convert method as well, but does not sign Itemperature. This method is responsible for intermediate between the main and the conversion classes. It receives two temperatures, DE and PARA, and also the value that will be converted. He uses classes this way:
public class Converter
{
public double Convert(double valor, ITemperatura de, ITemperatura para)
{
return de.Convert(valor, para);
}
}
Already everything ready, for consumption on main, I have there 2 cases, one of success and one of failure.
static void Main(string[] args)
{
// 1º caso (ok):
Celsius celsius = new Celsius();
double valor = celsius.Convert(100, new Farenheit());
Console.WriteLine(valor);
// 2º caso (entrou no ITemperatura, não na especializada):
Converter converter = new Converter();
valor = converter.Convert(100, new Celsius(), new Farenheit());
Console.WriteLine(valor);
Console.Read();
}
//outputs: 2
// 0
The first case is that of success, he creates an instance of Celsius and from this instance he calls the Convert to turn it into Farenheit. In doing this, it will enter the correct overload method in the Celsius class that has Farenheit parameter. resulting in 2 (return from Farenheit ok).
The second case is the error case. Now using the Convert class. In this case, it is no longer going straight to the Celsius class. Now, it is passing before by Converter, which receives 2 Itemperature, the DE and the PARA, each being a temperature, and only then sends to the Celsius Class to make its overload.
This way, when he calls it. Convert(value, to); I wanted it to take the same instance of Farenheit in the PARA I sent from main and forward to the Celsius class in its correct overload that has a Farenheit in the parameter.
However, when sending to the Celsius class, it does not do this, it forwards to the abstract method that receives Itemperatura, returning 0;
Important to note that, in Converter, I have the correct instance that I receive in main, and even later, when it sends to Celsius in the method that has Itemperatura, I debugged and there is the instance of Farenheit, but he nevertheless entered the generic method that uses Itemperatura.
Is there any way, even getting in the Convert TO as Itemperatura, to get you into the specific Celsius method without having to do if to test your specific type? Say something to the compiler like: "I want to call the most specific method for this class, always".
My intention is to learn. You can throw away my solution and say that there is another totally different way and that does not use ifs or explicit switch, it will be great to know. If you have a suggestion on top of this project it would also be interesting.
Thank you in advance for the analysis, Robson Faxas.
Please avoid long discussions in the comments; your talk was moved to the chat
– Maniero