【问题标题】:What step can I take to fix the warning of "Raw use of parameterized class 'Class' "?我可以采取什么步骤来修复“原始使用参数化类'Class'”的警告?
【发布时间】: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&lt;?&gt;[] dragonTypes = {Dragon.class, IceDragon.class}; 解决了您的问题吗?
  • @LajosArpad 这给出了“不兼容的类型”的编译错误。找到:'java.lang.reflect.Constructor>',需要:'java.lang.reflect.Constructor'"
  • 不能是private static final Class&lt;Dragon&gt;[] dragonTypes吗?
  • @Zannith,这给出了“不兼容的类型。找到:'java.lang.Class',需要:'java.lang.Class'”的编译错误。

标签: java class object oop inheritance


【解决方案1】:

不允许实例化泛型类型数组(除非类型参数是未绑定的通配符,例如 List>。This article 有解释。

因为你必须使用数组。您可以使用:

private static final Class<?>[] dragonTypes = {Dragon.class, IceDragon.class};

至于报错,将返回对象强制转换为Constructor&lt;Dragon&gt; constructor

@SuppressWarnings("unchecked")
Constructor<Dragon> constructor =(Constructor<Dragon>) dragonTypes[index].getConstructor(String.class, String.class);

您已经取消了警告。

【讨论】:

    【解决方案2】:

    TL;DR:你必须使用数组吗?如果没有,请使用列表

    替换这个:

    Constructor&lt;Dragon&gt; constructor = dragonTypes[index].getConstructor(String.class, String.class);

    有了这个:

    Constructor&lt;Dragon&gt; constructor = dragonTypes.get(index).getConstructor(String.class, String.class);


    还有这个:

    private static final Class[] dragonTypes = {Dragon.class, Dragon.class};

    有了这个:

    private static final List&lt;Class&lt;Dragon&gt;&gt; dragonTypes = Arrays.asList(Dragon.class, Dragon.class);

    【讨论】:

    • 我只能使用数组。
    • Morover,我收到“无法解析方法'get(int)'”、“无法解析符号'列表'”和“无法解析符号'数组'”的编译错误。跨度>
    猜你喜欢
    • 2021-08-26
    • 1970-01-01
    • 2016-12-22
    • 1970-01-01
    • 2012-08-06
    • 1970-01-01
    • 2021-12-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多