我可以采取什么步骤来修复“参数化类‘Class’的原始使用”的警告?

Aru*_*rya 7 java generics

在下面的程序中,我有类CowDragon从类派生的类CowIceDragon从类派生的类Dragon

还有一个HeiferGenerator类,负责创建一个包含CowDragon和的实例的数组IceDragon

但是,在HeifeferGenerator课堂上,我收到警告“参数化类'Class'的原始使用”:

private static final Class[] dragonTypes = {Dragon.class, IceDragon.class};
Run Code Online (Sandbox Code Playgroud)

我尝试查看Raw use ofparameterized 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;

  }
Run Code Online (Sandbox Code Playgroud)

onk*_*kar 2

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

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

private static final Class<?>[] dragonTypes = {Dragon.class, IceDragon.class};
Run Code Online (Sandbox Code Playgroud)

至于错误,将返回对象转换为Constructor<Dragon> constructor

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

您已经抑制了该警告。