【发布时间】:2021-06-27 15:57:36
【问题描述】:
我正在运行以下程序。在这个程序中,我有 Cow 类,从 Cow 类派生的 Dragon 类,以及从 Dragon 类派生的 IceDragon 类。 Cow 类、Dragon 类和 Ice dragon 类在一个名为 HeiferGenerator 的类中实现。我在一个名为 CowSay 的类中运行主函数,我在其中实现 HeiferGenerator 类以及 Cow 类、Dragon 类和 IceDragon 类。但是,请看下面的 HeiferGenerator 类。我收到警告“Raw use of parameterized class 'Class'”:
private static final Class[] dragonTypes = {Dragon.class, IceDragon.class};
我尝试查看Raw use of parameterized class 的答案,但没有任何帮助。我可以做些什么来解决这个警告?
// 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;
}
【问题讨论】:
-
糟糕的标题。重写以总结您的具体技术问题。
-
private static final Class<?>[] dragonTypes = {Dragon.class, IceDragon.class};解决了您的问题吗? -
@LajosArpad 这给出了“不兼容的类型”的编译错误。找到:'java.lang.reflect.Constructor
>',需要:'java.lang.reflect.Constructor'" -
不能是
private static final Class<Dragon>[] dragonTypes吗? -
@Zannith,这给出了“不兼容的类型。找到:'java.lang.Class
',需要:'java.lang.Class '”的编译错误。
标签: java class object oop inheritance