I am learning generics but can´t seem to understand very well how to use them what I am trying to do is create a method that divides 2 numbers, can someone give me a short but easy to understand explanation so I can better understand it.
I was able to do this with strings like this:
package aa4pedtlacaelel;
public class UsoGenericos <T, U>{
private T nombre;
private U apellido;
public void nombreCompleto (T nombre, U apellido){
this.nombre = nombre;
this.apellido = apellido;
System.out.println("Mi nombre es: " + nombre + " " + apellido);
}
}
However I still don´t really get why do it this way
So, I created a class that does divide the 2 numbers properly and was able to call it from a class within the same package successfully.
package aa4pedtlacaelel;
public class UsoGenericos {
public double divide (double divisor, double dividendo){
double resultado = dividendo / divisor;
return resultado;
}
}
and was trying to convert this to use generic types and I got this far
package aa4pedtlacaelel;
public class UsoGenericos <T, U, S>{
private T dividendo;
private U divisor;
public double divide (T dividendo, U divisor){
this.dividendo = dividendo;
this.divisor = divisor;
return dividendo / divisor;
}
}
but can´t seem to get pass the return value, is it possible to do what I am trying to? or generic types are meant for something other than this
[code=java]
public class UsoGenericos
{
private double nombre;
private double apellido;
public double nombreCompleto(double nombre, double apellido)
{
return nombre / apellido;
}
public static void main(String[] args)
{
UsoGenericos uso = new UsoGenericos();
System.out.println(uso.nombreCompleto(5,2/1));
}
}
well, thank you for your reply, however my point was on using generic types