En el siguiente programa, tengo la clase Cow
, la clase Dragon
derivada de la clase Cow
y la clase IceDragon
derivada de la clase Dragon
.
También existe la clase HeiferGenerator
, responsable de crear una matriz que contiene instancias de Cow
, Dragon
y IceDragon
.
Sin embargo, en la clase HeifeferGenerator
, recibo la advertencia "Raw use of parameterized class 'Class'" en la línea:
private static final Class[] dragonTypes = {Dragon.class, IceDragon.class};
¿Qué puedo hacer para corregir esta advertencia?
// Cow class
public class Cow {
// Declaring attributes name and image
private final String name;
private String image;
// Constructor to create a new Cow object with parameter name
public Cow (String name) {
this.name = name;
this.image = null;
}
// Accessor to return the name of the cow
public String getName() {
return this.name;
}
// Accessor to return the image used to display the cow after the message
public String getImage() {
return this.image;
}
// Mutator to set the image used to display the cow after the message
public void setImage(String _image) {
this.image = _image;
}
}
enter code here
// Dragon class derived from the Cow class
public class Dragon extends Cow {
// Constructor to create a new Dragon object with parameters name and image
public Dragon (String name, String image) {
super(name);
setImage(image);
}
// Function to return true for the default Dragon type
public boolean canBreatheFire() {
return true;
}
}
// IceDragon class derived from the Dragon class
public class IceDragon extends Dragon {
// Constructor to create a new IceDragon object with parameters name and image
public IceDragon (String name, String image) {
super(name, image);
}
// Function to return false for the IceDragon type
public boolean canBreatheFire() {
return false;
}
}
import java.lang.reflect.Constructor;
public class HeiferGenerator
{
public static Cow[] getCows()
{
if (cows == null)
{
cows = new Cow[cowNames.length + dragonNames.length];
// Add the "regular" cows
for (int index = 0; index < cowNames.length; index++)
{
cows[index] = new Cow(cowNames[index]);
cows[index].setImage(quoteLines + cowImages[index]);
}
// Add the dragons
for (int offset = cowNames.length, index = 0; index < dragonNames.length; index++)
{
try
{
@SuppressWarnings("unchecked")
Constructor<Dragon> constructor = dragonTypes[index].getConstructor(String.class, String.class);
cows[offset + index] = constructor.newInstance(dragonNames[index], quoteLines + dragonImage);
}
catch (Exception ignored) { }
}
}
return cows;
}
// Hard-coded values for some of the cows
private static final String[] cowNames = { "heifer", "kitteh" };
private static final String quoteLines = " \\\n" +
" \\\n" +
" \\\n";
private static final String[] cowImages = { " ^__^\n" +
" (oo)\\_______\n" +
" (__)\\ )\\/\\\n" +
" ||----w |\n" +
" || ||\n",
" (\"`-' '-/\") .___..--' ' \"`-._\n" +
" ` *_ * ) `-. ( ) .`-.__. `)\n" +
" (_Y_.) ' ._ ) `._` ; `` -. .-'\n" +
" _.. `--'_..-_/ /--' _ .' ,4\n" +
" ( i l ),-'' ( l i),' ( ( ! .-'\n"
};
private static final String[] dragonNames = { "dragon", "ice-dragon" };
private static final Class[] dragonTypes = {Dragon.class, Dragon.class};
private static final String dragonImage = " |\\___/| /\\ //|\\\\\n" +
" /0 0 \\__ / \\// | \\ \\\n" +
" / / \\/_ / // | \\ \\\n" +
" \\_^_\\'/ \\/_ // | \\ \\\n" +
" //_^_/ \\/_ // | \\ \\\n" +
" ( //) | \\ // | \\ \\\n" +
" ( / /) _|_ / ) // | \\ _\\\n" +
" ( // /) '/,_ _ _/ ( ; -. | _ _\\.-~ .-~~~^-.\n" +
" (( / / )) ,-{ _ `.|.-~-. .~ `.\n" +
"(( // / )) '/\\ / ~-. _.-~ .-~^-. \\\n" +
"(( /// )) `. { } / \\ \\\n" +
" (( / )) .----~-.\\ \\-' .~ \\ `. __\n" +
" ///.----..> \\ _ -~ `. ^-` \\\n" +
" ///-._ _ _ _ _ _ _}^ - - - - ~ `-----'\n";
private static Cow[] cows = null;
}
Traducción del inglés